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 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 ));
?>
share|improve this question
    
Elaborate on not order properly? How are they sorted in that case? –  Rarst Jun 22 '11 at 21:18
    
Its as if the query can not find 'menu_order' from the custom posts. The query currently is ignoring the argument. –  cnotethegr8 Jun 22 '11 at 22:15
    
Is it query for multiple post types? If you query in that way for native pages is menu_order applied? –  Rarst Jun 22 '11 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. –  cnotethegr8 Jun 22 '11 at 22:28

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
Thanks, i ended up getting this to work a while back. But i appreciate it! ;) –  cnotethegr8 Jul 25 '11 at 20:09
2  
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 Jul 24 '12 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 Aug 22 '12 at 16:12

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;
share|improve this answer

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

share|improve this answer

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.