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'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();
    $wp_query->query('posts_per_page=2&orderby='.$orderBy.'&paged='.$paged);
    ?>

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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

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.