I have to get some custom post types and need to do that with WP_Query (query_posts doesn't work).

How can I do the pagination? Whatever I tried didn't work... any help would be awesome I can't crack this alone...

$args = array(
'tax_query' => array(
   'posts_per_page' => 5,
    'author' => $user_id,
        'taxonomy' => 'custom_taxtype',
        'field' => 'slug',
        'terms' => 'delicious-food',
$answer_query = new WP_Query( $args );

//// Loop trough the results

while ( $answer_query->have_posts() ) : $answer_query->the_post();

//// do something

  • 1
    Add the paged => 1,2...n argument, and I think that posts_per_page, as well as paged should be outside of the tax_query array.
    – soulseekah
    Commented Oct 17, 2011 at 10:56
  • Thanks yeah just did this, now I can limit the posts. Thats a first step!
    – deltakid
    Commented Oct 17, 2011 at 11:27
  • Did you add paged and set it to the page number you are looking for?
    – soulseekah
    Commented Oct 17, 2011 at 11:30
  • I did set it but with it can just pick what page I want and can't get the pagination to work. I use the regular wordpress next_posts_link(); function for the pagination.
    – deltakid
    Commented Oct 17, 2011 at 11:33

2 Answers 2


If you could tell us what you're exactly trying to achieve, we can help you better!

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
               'post_type' => 'question',
               'posts_per_page' => -1,
               'paged' => $paged,
               'author' => $user_id
$my_query = new WP_Query($args);

        //Loop goes here...
    //support for page-navi plugin, please refer readme.txt for further instructions
    if ( function_exists('wp_pagenavi') )
    elseif ( get_next_posts_link() || get_previous_posts_link() )
        <div class="wp-navigation clearfix">
            <div class="alignleft"><?php //next_posts_link('&laquo; Older Entries'); ?></div>
            <div class="alignright"><?php //previous_posts_link('Newer Entries &raquo;'); ?></div>
<?php } //if wp_pagenavi

Check WP_Query documentation for more parameters.

Btw, query_posts also works with custom posts, as it takes all the parameters that you can pass to WP_Query.

  • Thanks Ruwick. I just want to get all the posts from a custom post format called questions and this custom post format has different categories that are defined as custom terms. With the query above I can get the latest posts for a certain custom term but can't get pagination to work.
    – deltakid
    Commented Oct 17, 2011 at 11:28
  • Have you installed WP-Pagenavi? I skipped that part, check the new code in my answer. I have added the pagination part. Commented Oct 17, 2011 at 11:35
  • Thanks a lot! Yes it works now. I modified the code a bit and used the code from layotte down here now. wordpress.org/support/topic/…
    – deltakid
    Commented Oct 17, 2011 at 11:45
  • Great job man! :) Commented Oct 17, 2011 at 11:46

IF you change the $my_query to $wp_query it should work. The code in the internal wordpress functions next_posts_link & previous_posts_link expect the query object to be called $wp_query.

  • 1
    By doing this you also clobber the main query which can result in unintended consequences. Can you edit the answer to explain how to avoid that problem? Also, it is best to post plausibly working code in the answer.
    – s_ha_dum
    Commented Mar 13, 2013 at 14:40
  • wow.. what a great tip!
    – Stavros
    Commented Oct 28, 2021 at 19:58

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.