1

I have written a WP_query with a date range.

            $args = array( 
                'date_query' => array( 
                    array(
                        'column' => 'post_date_gmt',
                        'after' => array('year' => $after[0], 'month' => $after[1], 'day' => $after[2])
                    ),
                    array (
                        'column' => 'post_date_gmt',
                        'before' => array('year' => $before[0], 'month' => $before[1], 'day' => $before[2])
                    )
                ),
                'posts_per_page' => -1,
                'inclusive' => false,
            );

            $posts = new WP_Query( $args ); 

which works perfectly when I output the sql and run it in phpmyadmin.

SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND ( wp_posts.post_date_gmt > '2015-01-11 23:59:59' 
AND wp_posts.post_date_gmt < '2015-01-17 00:00:00' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 
ORDER BY wp_posts.post_date DESC

However the loop prints out more posts

if ( $posts->have_posts() ) : 
   while ( $posts->have_posts() ) : $posts->the_post();

I have tried using the reset loop functions but with no luck. What am I doing wrong. This is in a page template that is trying to mimic archive.

Thanks

Chris

6
  • Try to dump the whole $posts object and see what's going on there.
    – Rarst
    Jan 28, 2015 at 21:45
  • The whole posts object shows the additional posts. davies-barnard.uk/newsletters Pick the 2015/01/17 and click go. You'll see no. of posts returned as 3 but not in posts. Jan 28, 2015 at 21:47
  • This is highly peculiar. You got 3 found_posts, but 11 posts_count, this seems plain invalid. There can't be more results than there can possibly be results. Is anything else at all possibly manipulating the $posts variable? Can you try this with plugins disabled.
    – Rarst
    Jan 28, 2015 at 21:59
  • Will do. Does it make any difference for a WP Network? (Thanks again!) Jan 28, 2015 at 22:00
  • No change - I just disabled the plugins. I'm glad you are also surprised! Would it make any difference that this is a page template pretending to be a weekly archive? Jan 28, 2015 at 22:05

1 Answer 1

4

Try to run your query with

"suppress_filters" => true

WP_Query allows for posts to be added after the query has run. found_posts is the actual queried amount of posts, post_count is updated after running the filters.

If this fixes it search your code (Any active code, check network activated plugins as well) for the_posts or posts_results filter and check if that is causing it.

Another reason for this could be sticky posts as we discovered in the comments, these can be disabled using

"ignore_sticky_posts" => true

By default WP_Query adds sticky posts to the first page of archives where is_home() is true.

5
  • Hi, thanks for the response. I have added both suppress filters ... and outputted the post count, which is 3 vs 11. Sadly it has NOT fixed the problem :( Jan 28, 2015 at 22:54
  • The posts it is adding are all those stickies from the homepage. Is it adding these to every query? Jan 28, 2015 at 22:55
  • 1
    Oh yeah, sticky posts, never use them ;). You can remove them using ignore_sticky_posts => true Jan 28, 2015 at 22:56
  • You are a LEGEND!!! Love stickies except when they waste an evening of my life that could have been spent with my wife. BIG THANKS! Jan 28, 2015 at 22:58
  • I'll add it to my answer. Jan 28, 2015 at 22:59

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.