3

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.

1 Answer 1

2

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.

2
  • 3
    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'. Jan 11, 2012 at 18:22
  • @Stephen Yes, this is intended, but good of you to point out that this is not a generic solution. +1 Jan 11, 2012 at 20:50

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.