0

It's seemingly simple, I have this loop which should loads posts from, and including the current post.

<?php
$categories = get_the_category();
foreach( $categories as $category ) {
  $catID[] = $category->cat_ID;
} 
$args = array(
    'category__in' = > $catID,
    'category__not_in' = > 1,
    'posts_per_page' = > 10,
    'date_query' = > array(
        array(
            'before' = > get_the_date()
        ),
        'inclusive' = > true, 
    )
);
$loop = new WP_Query($args);
while ($loop -> have_posts()) {
$loop -> the_post();
  get_template_part( 'sidebar-posts-template' );
}
wp_reset_postdata();
?>

The problem is that it doesn't include the current post. Why?

Edit: This loop, using a fixed date also does not work and will start loading posts from the 27th, not including the 28th. The inclusive date should include posts from the 28th.

$args = array(
    'date_query' = > array(
        array(
            'after' = > 'January 1st, 2013',
            'before' = > array(
                'year' = > 2013,
                'month' = > 2,
                'day' = > 28, 
             ),
            'inclusive' = > true, 
        ), 
    ),
    'posts_per_page' = > -1, );

Edit 2: This is now the loop I am using.

$args = array(
    'category__in' => $catID,
    'category__not_in' => 1,
    'posts_per_page'=>10,
    'date_query' => array(
        array(
            'before' => get_the_date(),
            'inclusive' => true, 
        ),
    )
);

Edit 3:

$args = array(
    'posts_per_page'=>10,
    'date_query' => array(
        array(
          'before'    => array(
         'year'  => 2013,
         'month' => 2,
         'day'   => 28,
           ),
          'inclusive' => true 
        ),
     )
);
1
  • 1
    Please take better care with your syntax. Your issue is that you have the inclusive parameter inside the wrong array, which is readily apparent when you use proper code indentation. Apr 7 '14 at 13:42
4

Edit 3

Now, combine Edits 1 and 2:

'date_query' = > array(
    array(
        'before' = > strtotime( get_the_date() ),
        'inclusive' = > true, 
    )
)

Edit 2

You don't have your inclusive parameter inside the correct array.

Change this:

'date_query' = > array(
    array(
        'before' = > get_the_date()
    ),
    'inclusive' = > true, 
)

...to this:

'date_query' = > array(
    array(
        'before' = > get_the_date(),
        'inclusive' = > true, 
    )
)

Edit

The get_the_date() function must be used inside the loop, because it relies on the $post global.

The get_the_date() function also returns a date-formatted string, but the date_query before parameter accepts a strtotime() compatible string.

Instead of this:

'before' => get_the_date()

...try this:

'before' => strtotime( get_the_date() )

Original Answer

The inclusive parameter is part of the WP_Query() date_query parameter, and must be used inside of the array passed to date_parameter. Refer to the example usage in the Codex:

$args = array(
    'date_query' => array(
        array(
            'after'     => 'January 1st, 2013',
            'before'    => array(
                'year'  => 2013,
                'month' => 2,
                'day'   => 28,
            ),
            'inclusive' => true,
        ),
    ),
    'posts_per_page' => -1,
);
$query = new WP_Query( $args );
23
  • Hi, Chip, that's the way I originally done it but ended up fiddling with it because it didn't work. If I use that exact same query in your answer my posts start from the 27th instead of the 28th so the inclusive still doesn't work
    – UzumakiDev
    Apr 7 '14 at 13:22
  • Your date_query syntax was wrong. Please see edit. Apr 7 '14 at 13:31
  • Hi, Chip, yes I've noticed that but even when I used a fixed date, like the one in the example, it still does not work which seems to me like a pretty big problem. I'll edit my question to show you the exact arguments I'm using now.
    – UzumakiDev
    Apr 7 '14 at 13:36
  • The problem remains the same: the inclusive parameter is inside the wrong array. Syntax, including code indentation, is critical to debugging. Apr 7 '14 at 13:42
  • 2
    Please take extensive discussion to chat, this is too much for comments.
    – Rarst
    Apr 7 '14 at 17:33
0

I had problems using get_the_date() so I ended up getting the post date as it is in the database.

<? php
global $post;
$postTime = $post -> post_date;
$args = array(
    'category__in' => $catID,
    'category__not_in' => 1,
    'posts_per_page' => 10,
    'date_query' => array(
array(
    'before' => $postTime,
    'inclusive' => true 
), ));
$loop = new WP_Query($args);
while ($loop -> have_posts()) {
    $loop -> the_post();
    get_template_part('sidebar-posts-template');
}
wp_reset_postdata(); ?> 

The important piece of code is

global $post;
$postTime = $post -> post_date;

The variable $postTime is what made inclusive work.

4
  • "get_the_date is not an acceptable value for strtotime* - this isn't actually true. strtotime() will take any properly formatted date/time string. get_the_date( $d ) will output whatever date/time string you specify, as $d. The default value is get_option( 'date_format' ), which is why I asked you what that default value was. Apr 7 '14 at 19:38
  • Ah, I don't understand what you mean. So, what am I supposed to put into get_the_date() to get a format that works with strtotime and inclusive? Could you give me an example?
    – UzumakiDev
    Apr 7 '14 at 19:48
  • strtotime() should take any properly formatted date/time string. If it were me, I would use get_the_date( get_option( 'time_format' ) ). Apr 7 '14 at 20:01
  • Use get_the_date( 'c' ) if you need a string format that will be easily parsable by the strtotime function. This will produce an ISO 8601 formatted date string, and that will eliminate the guesswork involved.
    – Otto
    Apr 7 '14 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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