Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using wp_list_pages to create a navigation menu. I've run into a challenge with the menu order, though, and I am trying to figure out a way to take more control over the order of the menu.

Is it possible to customize the order of the wp_list_pages output using a Walker?

For example, I'd like to check if a given page in wp_list_pages results has a post_meta value of page_x and output that first, then do the same for another page, then, if none of the rules match, continue as normal.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

wp_list_pages( $args ) calls get_pages( $args ). You can filter the get_pages() output with a filter on get_pages.

Let’s say you call wp_list_pages() like this:

wp_list_pages(
    array(
        'please_filter_me' => TRUE
    )
);

You can sort the pages now with code like this (not tested):

add_filter( 'get_pages', function( $pages, $args ) {
    // not our query
    if ( empty ( $args['please_filter_me'] ) )
        return $pages;

    $out = $top = array();

    foreach ( $pages as $page )
    {
        if ( get_post_meta( $page->ID, 'my_key', TRUE ) )
            $top[] = $page;
        else
            $out[] = $page;
    }

    return $top + $out;
}, 10, 2 );
share|improve this answer
    
This looks awesome. Been a long time since I had this question and I think I rolled a work-around.. I'm going to accept it for posterity, though, and reference back when I run into this again. –  Jonathan Wold Jan 21 at 0:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.