0

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.

2

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.

0

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

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.