7

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.

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 ));
?>
4
  • Elaborate on not order properly? How are they sorted in that case?
    – Rarst
    Commented Jun 22, 2011 at 21:18
  • Its as if the query can not find 'menu_order' from the custom posts. The query currently is ignoring the argument. Commented Jun 22, 2011 at 22:15
  • Is it query for multiple post types? If you query in that way for native pages is menu_order applied?
    – Rarst
    Commented Jun 22, 2011 at 22:18
  • It is for a single post type. And all the arguments are proper. I'll update the code question with my code for the query. Commented Jun 22, 2011 at 22:28

3 Answers 3

16

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.

4
  • Thanks, i ended up getting this to work a while back. But i appreciate it! ;) Commented Jul 25, 2011 at 20:09
  • 7
    In case anyone else is scratching their heads over this, it's the presence of the 'order' argument that is required to make it work. I had set 'order_by' => 'menu_order' and it didn't work until I also added 'order' => 'ASC'
    – jessica
    Commented Jul 24, 2012 at 23:17
  • I was having trouble with get_posts with Wordpress 3.4.1. Didn't start working until I shifted to using query_posts
    – acorncom
    Commented Aug 22, 2012 at 16:12
  • 2
    Can confirm - March 2018, still fails without the 'order' => 'ASC' part as well as the orderby clause. Weird, but, there ya go.
    – Eric
    Commented Mar 22, 2018 at 14:01
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

0

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;

Your Answer

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

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