1

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.

3
  • unrelated but there is no cleanup code after this, you have to call wp_reset_postdata() after your loop (immediatley after endwhile; ) or it will contaminate code that runs afterwards. Also there is no handling for when get_field fails or the field isn't present or hasn't been set yet or its value is an object/array/etc
    – Tom J Nowell
    Dec 31, 2021 at 1:52
  • Thank you @TomJNowell I do have the wp_reset_query(); on the next line, I should have included that. And then if get_field is empty, it'll return null and then be handled by the else part of this function. Maybe there is a more elegant way to do this?
    – opendatum
    Dec 31, 2021 at 2:30
  • it won't be handled by the else part, you'll trigger database queries searching for meta with the value null, 'meta_value' => null, doesn't mean it won't find anything
    – Tom J Nowell
    Dec 31, 2021 at 11:27

1 Answer 1

1

Couple of things to consider here:

1.) Try adding the meta query only if the value is present:

$pairedThing = get_field('myThing');
$args = [
    'posts_per_page' => 2,
    'post_type'      => 'thing',    
];
if ( ! empty( $pairedThing ) ) {
    $args['meta_key']   = 'identifier';
    $args['meta_value'] = $pairedThing;
}

2.) Account for posts without the meta-key set:

$pairedThing = get_field('myThing');
$args = [
    'posts_per_page' => 2,
    'post_type'      => 'thing',
    'meta_query'     => [
        'my_meta_query' [ // Multiple meta-queries per handle.
            'relation' => 'OR',
            [
                'key'     => 'identifier',
                'value'   => $pairedThing,
                'compare' => '=',
            ],
            [
                'key'     => 'identifier',
                'compare' => 'NOT EXISTS',
            ],
        ],
    ]
];
1
  • 1
    Option 1 worked! I guess the lack of an alternative (in this case ! empty) to the query failing meant it had to produce results. Sort of makes sense?? Thank you!
    – opendatum
    Dec 31, 2021 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.