How do I use fields => ids in an array with WP Query?

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;

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)