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'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.

share|improve this question
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

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

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.