1

In my example below, when using fields => ids https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter to get an array of post IDs, why does var_dump($args->posts) show NULL when WP_Query($args) does work in my posts loop?

This is an 10 year old question an answer that I'm using as reference Get post ids from WP_Query? and the accepted answer does not work for me.

$one_year_ago = date('F j, Y', strtotime('-364 days'));
$one_year_ago_plus_four = date('F j, Y', strtotime('-369 days'));

$args = array(
      'post_type' => 'post',
      'posts_per_page' => 5,
      'post_status' => 'publish',
      'orderby' => 'date',
      'order' => 'DESC',
      'fields' => 'ids',
      'date_query' => array(
            array(
                'before'    => $one_year_ago,
                'after'     => $one_year_ago_plus_four,
                'inclusive' => true,
            ),
       ),
 );

// Why is $args null?
var_dump($args->posts);
?>

// My post loop works:
<?php $query = new WP_Query($args);
while($query->have_posts()) : $query->the_post(); .....

Edit 10/10/25:

With Ruchita Sojitra's answer, var_dump($query->posts); now shows this:

array(5) { [0]=> int(274354) [1]=> int(274243) [2]=> int(274240) [3]=> int(274225) [4]=> int(274221) }

So that works. But my end goal is to have a string of post IDs as a variable, but using this:

$post_ids_string = implode( ',', $query );
echo $post_ids_string;

throws the error implode(): Argument #2 ($array) must be of type ?array.

Update: Thanks to Philipp Zedler, this works: $post_ids_string = implode( ',', $query->posts );

1
  • Because you're passing $query to implode but you'r dumping $query->posts. You need to implode $query->posts. Commented Oct 14 at 11:16

1 Answer 1

3

Why is $args null?

  • $args is just a plain PHP array of query arguments.
  • Arrays in PHP do not have properties (like $args->posts).
  • The ->posts property only exists after you instantiate a WP_Query object.
  • So at that point in your code, $args->posts will always be null or throw an error, because $args is not a WP_Query instance.

Try below the code.

$one_year_ago_start = date('Y-m-d', strtotime('-369 days'));
$one_year_ago_end   = date('Y-m-d', strtotime('-364 days'));
$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 5,
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
    'fields'         => 'ids',
    'date_query'     => array(
        array(
            'after'     => $one_year_ago_start,
            'before'    => $one_year_ago_end,
            'inclusive' => true,
        ),
    ),
);

$query = new WP_Query($args); var_dump($query->posts);

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); echo get_the_title(); endwhile; wp_reset_postdata(); endif;

3
  • Thanks! That works. My mistake. But what I am trying to do is get a string of post IDs, and now I see implode(): Argument #2 ($array) must be of type ?array Commented Oct 10 at 14:50
  • 1
    Try: $post_ids_string = implode( ',', $query->posts ); Commented Oct 10 at 15:35
  • @PhilippZedler Thanks! That worked. Commented Oct 10 at 17:30

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.