Vladimir
Vladimir

Reputation: 453

How to order multidimensional arrays PHP

I've been stuck for hours in trying to order get_pages() object from wordpress.

I need to order the pages by menu_order (get_pages->menu_order);

I've been trying several PHP functions but none worked in such purpose.

PHP

foreach(get_pages(pll_current_language()) as $page) { 

    // Somewhere here each page should go up/down in order to be DESC/ASC

    $template = get_post_meta( $page->ID, '_wp_page_template', true ); 
    include_once($template);  

}

My question is, How can I order this object by one of its values, example:

I want to order get_pages() by get_pages()->menu_order

Any help will be appreciated.

Upvotes: 0

Views: 58

Answers (2)

user4962466
user4962466

Reputation:

just use the $args options array and set the sort_order, sort_column on whatever you need, check the docs https://codex.wordpress.org/Function_Reference/get_pages

$args = [
    'sort_order' => 'asc',
    'sort_column' => 'menu_order'
];

$pages = get_pages($args);

Upvotes: 3

Chris
Chris

Reputation: 5876

You probably want to use the usort function. You provide it with a comparison function (in your case a function that compares $a->menu_order to $b->menu_order) and it will then use it to sort the elements in your array.

Upvotes: 1

Related Questions