I am having a problem with the custom category template pagination and i search and read that using pre_get_posts fix the pagination problem. I read the pre_get_posts information in the WordPress Codex but i didn't understand how to use it.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
            'post_type' => 'post',
            'meta_key' => 'hmeromhnia_lhkshs',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            'posts_per_page' => 9,
            'paged' => $paged

// get results
query_posts( $args );?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>

// The Loop

<?php endwhile;?>
<?php endif; ?>

<?php if ($wp_query->max_num_pages > 1) tie_pagenavi(); ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

This is the code i use for the query and the pagination doesn't work. I can't understand where and when to insert the pre_get_posts. Generally don't know how to use it.

Please help me!

  • 1
    Have you read our topics for that tag?
    – fuxia
    Jun 11, 2013 at 19:24
  • Yes i read them and i was searching and testing in my site for about 3 hours. It seems that there are different cases for pre_get_posts and pagination issues..
    – Charitos
    Jun 11, 2013 at 19:34
  • 1
    Then add your code to your question. Be specific.
    – fuxia
    Jun 11, 2013 at 19:34
  • Here's one solution that comes up in the search. Jun 11, 2013 at 19:58
  • 1
    Could you please stop stuffing things in the comments and instead update your answer? :)
    – kaiser
    Jun 11, 2013 at 23:58

1 Answer 1


Remove all query code from the template and just leave the default loop. In your example code you're overwriting the query in the template, in the code you pasted in the comments, you're running an entirely new query. These are both unnecessary when using pre_get_posts.

Put your pre_get_posts code in functions.php. You don't have to pass $paged or do anything fancy except set the specific parameters you want to modify, this code is running before the query happens:

function pregp_wpse_102658( $qry ) {
    if ( $qry->is_main_query() && $qry->is_category( 'expiresoon' ) ) {

and then just use the plain vanilla loop in the template:

if ( have_posts() ) :
    while ( have_posts() ) : the_post();  
        // The Loop

If there is nothing else special about this particular category, you no longer need a category template specific to that one category, since the query modification now happens outside the template.

  • i inserted the code and now it doesn't display any of the posts.
    – Charitos
    Jun 11, 2013 at 21:38
  • 1
    are you sure you removed everything unnecessary from the template? are you sure meta key is correct and is associated to posts? are you sure category slug is correct?
    – Milo
    Jun 11, 2013 at 21:49

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.