I have a WP Query that should return no results, but instead seems to be returning items. I'm guessing the null result is being interpreted as "return all results" but I'm not sure the proper fix. It looks like this:
$pairedThing = get_field('myThing');
$args = array(
'posts_per_page'=> 2,
'post_type' => 'thing',
'meta_key' => 'identifier',
'meta_value' => $pairedThing,
);
$the_query = new WP_Query( $args ); ?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<tr><td>Name</td><td>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</td></tr>
<?php endwhile; else:
//{echo 'None';}
?>
<?php endif; ?>
Each query will either have a single result (if there is a match to the query) or no results. When there is a match everything works as planned, but when there is no match the most recent result in the post_type are return, when I would expect nothing to be returns.
Any ideas? I trued the "else" but it didn't work.
wp_reset_postdata()
after your loop (immediatley afterendwhile;
) or it will contaminate code that runs afterwards. Also there is no handling for whenget_field
fails or the field isn't present or hasn't been set yet or its value is an object/array/etcwp_reset_query();
on the next line, I should have included that. And then ifget_field
is empty, it'll return null and then be handled by theelse
part of this function. Maybe there is a more elegant way to do this?'meta_value' => null,
doesn't mean it won't find anything