Query menu_order custom post types

I added 'supports' => 'page-attributes' to my custom post type, and now i have the meta box for page ordering.

When i use the argument 'sort_column' => 'menu_order' with get_pages() on my custom post type, everything orders properly.

Read More

So why do the custom pages not order properly when i query the posts using query_posts(array('orderby' => 'menu_order'))

And what can i do to get them to order by the menu order?

Here is the query im using incase anyone wants to see it.

<?php 
    $current_term = ($wp_query->query_vars['section'] <> '') ? $wp_query->query_vars['section'] : $menu_arr[0]; 
    query_posts(array(  'post_type' => 'module', 
        'orderby' => 'menu_order',
        'tax_query' => array(array( 'taxonomy' => 'section', 
                                    'field' => 'slug', 
                                    'terms' => $current_term )),
        'post_parent' => 0 ));
?>

Related posts

Leave a Reply

3 comments

  1. I’ve just had to do the same thing as you, here is what I did to get this working:

    'supports' => array('title', 'editor', 'thumbnail', 'page-attributes')

    Register the post type with supports of page attributes. This adds the menu order meta box to the edit screen. From there you can place the order.

    Then run my custom query:

    $args = array(
        'numberposts' => -1,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post_type' => 'staff'
    );
    $staff = get_posts($args);
    

    set orderby to menu_order and order to ASC. Remember if you do not set a value in menu order it sets it to 0. So any posts without an order set will appear first.

  2. in my functions.php i used this:

    add_action( 'init', 'create_Videos' );
    function create_Videos() {
        register_post_type('videos', array(
            'label' => __('Videos'),
            'singular_label' => __('Video'),
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'article',
            'hierarchical' => true,
            'rewrite' => false,
            'query_var' => true,
            'supports' => array('title', 'editor', 'page-attributes')
        ));
    }
    

    and in my theme that:

        $args = array(
            'numberposts' => -1,
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'post_type' => 'videos'
        );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
    

    This is what worked for me
    Bye

  3. sort_column=menu_order only sorts pages based on their order in writing not the order you set in view > menus (translated), if you want that you can do like this:

    $children = get_pages('child_of='. $topID); 
    
    // 'sort_column=menu_order' <-- only sorts by post order in writing mode (page > edit) not the menu order set in view > menus
    // wp_nav_menu has what we need, let's sort it the same way.
    $options = array(
        'container' =>  '',
        'echo'      =>  false,
    );                      
    $nav = wp_nav_menu($options);       
    $nav = strip_tags($nav);        
    $nav = str_replace("r", '', $nav);
    $nav = explode("n", $nav);
    //print_r($nav);
    $newChildren = array();
    foreach ($nav as $item) {
        $item = trim($item);
        $run = true;
        for ($c = 0; $c < count($children) && run; $c++) {              
            $child = $children[$c];
            if (strcmp($child->post_title, $item) == 0 && !in_array($child, $newChildren)) {
                $newChildren[] = $child;                    
                $run = false;
            }
        }
    
        // Adding the children the nav_menu is lacking, not sure why not all sub-children 
        //  are added to the first child here..(works but don't know why :/)
        if ($run == true) {
            for ($c = 0; $c < count($children) && run; $c++) {              
                $child = $children[$c];     
                if (!in_array($child, $newChildren)) {
                    $newChildren[] = $child;
                }
            }           
        }
    }
    $children = $newChildren;