I cannot figure out why my WP_Query
always displays all my published posts regardless of what I put in the arguments.
<?php
$args = array('numberposts' => 1,
'meta_key' => 'display',
'meta_value' => 'about'
);
$about_preview_query = new WP_Query($args);
if ($about_preview_query->have_posts()) {
print "<h1>FOUND POSTS</h1>";
}
while ($about_preview_query->have_posts()) {
$about_preview_query->the_post();
print "<h1>";
the_title();
print "</h1>";
}
?>
Am I doing something wrong here? After readings the docs on WP_Query()
I cannot figure out where I’m going wrong, appreciate any help I can get.
Update
I’ve tried this code and I am still getting the same response. All the posts are being returned in the loop.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'display',
'value' => 'about',
'compare' => '=',
)
),
);
Update 2
It seems if I print $about_preview_query->found_posts
the output is 1. So I suspect there is something wrong with how I am looping the posts:
<?php if ($about_preview_query->have_posts()): ?>
<h1>Has <?php print $about_preview_query->found_posts ?> Posts</h1>
<?php while ($about_preview_query->have_posts()): $about_preview_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>