0

I have the below array that has been generated from wordpress using get_children(). The problem is that it can't seem to order the output by the menu order used in wordpress.

So the bellow array needs sorting by the inner value of the key [menu_order]. I've tried a number of different ways using usort etc, but can't seem to get it to work.

Array
(
[40] => WP_Post Object
    (
        [ID] => 40
        [post_author] => 1
        [post_date] => 2016-09-03 19:31:25
        [post_date_gmt] => 2016-09-03 19:31:25
        [post_content] => test 2
        [post_title] => Test 2
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => test-2
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2016-09-03 19:56:04
        [post_modified_gmt] => 2016-09-03 19:56:04
        [post_content_filtered] => 
        [post_parent] => 2
        [guid] => http://example.com/2
        [menu_order] => 2
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

[38] => WP_Post Object
    (
        [ID] => 38
        [post_author] => 1
        [post_date] => 2016-09-03 19:23:18
        [post_date_gmt] => 2016-09-03 19:23:18
        [post_content] => test 1
        [post_title] => Test 1
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => test-1
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2016-09-03 19:51:17
        [post_modified_gmt] => 2016-09-03 19:51:17
        [post_content_filtered] => 
        [post_parent] => 2
        [guid] => http://example.com/1
        [menu_order] => 1
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
)
1
  • Use usort and provide a custom function (preferably a closure) which compares the element. Commented Sep 3, 2016 at 20:23

3 Answers 3

1

Although you could use usort with a callback, the right solution would be to request items directly ordered via get_children(). You can achieve this by using the orderby argument. Like this:

$children = get_children(array(
  // other args here
  'orderby' => 'menu_order'
));

See WordPress' get_children() and get_posts() for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd like to point out that you cannot use 'orderby' with get_children()
@David Cassidy: Since get_children() implementation actually forwards its arguments to get_posts(), it supports all arguments supported by get_posts(). See get_children() implementation. The documentation of get_children() also says "Note: See get_posts() for a full list of $args parameters."
0

This is a typical usecase for usort

usort($array, function($a, $b){
    return $a['menu_order'] > $b['menu_order'];
});

2 Comments

You are supposed to return 0, 1 or -1.
Works either way.
0

You can use usort: http://php.net/usort . I tried to make it more details as below.

function menu_order($a, $b)
 {
    return strcmp($a->menu_order, $b->menu_order);
 }

usort($newsortedarray, "menu_order");

foreach ($newsortedarray as $array){

  // continue....

 }

3 Comments

Pretty sure strcmp will sort 1, 11, 2, instead of 1, 2, 11
This worked a treat. What would be better to use instead of strcmp?
@DavidCassidy you can also try strcasecmp — Binary safe case-insensitive string comparison if you want to make case insensitive. For more:php.net/manual/en/function.strcasecmp.php Here is nice explanation for both of those funcrions: hackingwithphp.com/4/7/15/comparing-strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.