I'm on a Category.php page which does the loop like this:

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

I usually do a custom loop like this:

<?php //Define the Loop
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();

I want to add the orderby variable to the first loop (the one on category.php) but can't figure out how to implement it.


You can use the pre_get_posts action hook to set the order on your category archive like so:

add_action('pre_get_posts', 'filter_category_orderby');

function filter_category_orderby( $query ){
    if( $query->is_category()){
        $query->set('orderby', 'title');

just paste this snippet in your theme's functions.php and you should be fine.

| improve this answer | |

If it's the main loop, you could use query_posts to add the orderby variable.

For example, you could order by title by putting this PHP code above the loop :

 global $query_string;

 query_posts( $query_string . 'orderby=title' );

 //Your while here
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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