WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have my front page setup to display a custom post type via:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

    if ( is_home() && false == $query->query_vars['suppress_filters'] )         $query->set( 'post_type', array( 'jwf_portfolio', 'attachment' ) );
                $query->set( 'order', 'menu_order' );

    return $query; }

What's the most efficient way to have these ordered by the number value in the Order input in the Attributes metabox for each custom post? Currently I'm trying

<?php query_posts( $query_string . '&orderby=menu_order' ); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

on my index.php and that's not cutting it.

share|improve this question
up vote 1 down vote accepted

This did it:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

    if ( is_home() && false == $query->query_vars['suppress_filters'] )
        $query->set( 'post_type', array( 'jwf_portfolio', 'attachment' ) );
                $query->set('orderby', 'menu_order');
                $query->set('order', 'ASC'); 

    return $query;

No need to mess with index.php now.

share|improve this answer
This does what you want it to, but it does a lot more. Any query will automatically order by 'menu_order'. Unless this is intended, you should make sure those $query->sets are wrapped up in the IF statement. Also, as a warning to people who would copy this solution: any query on the home page (which doesn't go through filters) will return only 'jwf_portfolio' or 'attachment'. – Stephen Harris Jan 11 '12 at 18:22
@Stephen Yes, this is intended, but good of you to point out that this is not a generic solution. +1 – two7s_clash Jan 11 '12 at 20:50

Your Answer


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.