0

In my custom query, every time I query for posts from a particular category using WP_Query() and the category has 10 posts for example, I seem to be missing a post. So querying for 10 posts only returns 9, querying for 11 posts only returns 10 and so on. Does anyone know why I am always missing one post in my query results?

This is the code I am using that is not working. Some of my other code tests have returned the same results.

if($carouselOnOff){?>
<div id="carouselContainer">
<h2 id="sponsorsTitle"><?php echo get_cat_name( $carouselCategory ) ?></h2>
<ul id="carousel">
    <?php $showPostsInCategory = new WP_Query(); $showPostsInCategory->query('cat='. $carouselCategory .'&showposts='. $carouselNumber .'');
    if ($showPostsInCategory->have_posts()) : while ($showPostsInCategory->have_posts()) : $showPostsInCategory->the_post();?>
        <li>
            <?php $data = get_post_meta( $post->ID, 'key', true ); ?>
            <a href="<?php  if ($data[ 'custom_link' ]) { echo $data[ 'custom_link' ];} else { the_permalink(); } ?>">
                <?php the_post_thumbnail('sponsor', array('title' => "")); ?>
            </a>
        </li>
    <?php endwhile; endif; ?>
</ul><!--end carousel-->
</div><!--end carouselContainer-->
<?php } ?>
5
  • 1
    Where is this happening? Homepage? All pages? Archive page? How about the code you're using to query the posts, can you put that up?
    – mor7ifer
    Commented Mar 11, 2012 at 16:28
  • append on homepage !... did not test it elsewhere
    – menardmam
    Commented Mar 11, 2012 at 20:09
  • 2
    Try changing 'cat='. $carouselCategory .'&showposts='. $carouselNumber .'' to 'cat='. $carouselCategory .'&posts_per_page='. $carouselNumber .'&offset=0'.
    – mor7ifer
    Commented Mar 11, 2012 at 20:12
  • 1
    I made some edits to your question, pending approval of course. I hope you don't mind. Commented Mar 11, 2012 at 22:32
  • OUPS.... i just start from scratch... and then BANG... it append, the output was FINE all the post was there, but after going through jQuery carousel... one is missing !... have to find out why, but in the carousel NOT in the request !
    – menardmam
    Commented Mar 12, 2012 at 3:59

5 Answers 5

1

It sounds like you have a filter running on that page that's adding an offset parameter to your WP_Query.

There aren't any really easy ways to search for all filters. The plugin Hikari Hooks is the simplest that I've seen. If you install and activate that plugin and view the problem page, you should be able to view any filters on request or pre_get_posts or other hooks that might be affecting your output. Then its just a matter of tracking down where they're being added and disabling them before your carousel query.

2
  • Note that the plugin Hikari Hooks is no longer maintained and is not compatible with Wordpress in 2021
    – wickywills
    Commented Jul 8, 2021 at 14:30
  • 1
    Good point, this answer is about 10 years old. I'd recommend Query Monitor these days for inspecting the actions and filters run on a page request. Commented Aug 9, 2021 at 18:57
0

It sounds like a browser caching issue possibly. If you're on a PC try holding down Ctrl+Alt+F5 to do a hard refresh and clear all caches.

3
  • not a caching.. for sure !... no pc either !
    – menardmam
    Commented Mar 11, 2012 at 16:23
  • 1
    Are you saying that the 10th won't show at first, or it won't ever show until an eleventh is added? IF you add an 11th post is it still only showing 10 meaning that the would be 11th post still doesn't appear? How many posts do you have set to show per page? Commented Mar 11, 2012 at 16:27
  • Eactly... no way to display the #10 post until #11 is creatred, but #11 dont show when created, until #12.. an so on ...
    – menardmam
    Commented Mar 11, 2012 at 16:41
0

I was having this same issue, inside the query I had a number set to 1, which I then changed to zero, and presto! this is my new code:

<?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 0 ) { ?>
0

I was having a similar problem. I'm still not sure exactly what was causing it, but I was able to patch it by forcing a large 'posts_per_page' variable in the query and setting the offset to 0.

$the_query = new WP_Query(array('post_type' => 'items', 'offset' => '0', 'posts_per_page' => '9999'));
0

It might be status issue. Try with:

'post_status' => 'any'

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.