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 );
$querytoimplodebut you'r dumping$query->posts. You need to implode$query->posts.