0

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,
    array(
    '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

endwhile;
4
  • 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
    Oct 17, 2011 at 10:56
  • Thanks yeah just did this, now I can limit the posts. Thats a first step!
    – deltakid
    Oct 17, 2011 at 11:27
  • Did you add paged and set it to the page number you are looking for?
    – soulseekah
    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
    Oct 17, 2011 at 11:33

2 Answers 2

2

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

<?php
$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);

if($my_query->have_posts()):
    while($my_query->have_posts()):$my_query->the_post();
        //Loop goes here...
    endwhile;
    //support for page-navi plugin, please refer readme.txt for further instructions
    if ( function_exists('wp_pagenavi') )
    {
        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>
        </div>
<?php } //if wp_pagenavi
endif;
?>

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.

4
  • 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
    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. 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
    Oct 17, 2011 at 11:45
  • Great job man! :) Oct 17, 2011 at 11:46
3

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.

2
  • 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
    Mar 13, 2013 at 14:40
  • wow.. what a great tip!
    – Stavros
    Oct 28, 2021 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.