0

OR operator is not working for both conditions, only check first value if true then code executed and if first is false then not check 2nd condition and return else output.

I also try with LIKE compare, but then it's check custom fields value with all other values and if This CF value's contain by any other string CF value then it's become true.

<?php
    $posts = get_posts(array(
    'numberposts' => 14,
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'travel',
    'meta_query' => array(
        'relation' => 'OR',
          array(
            'key'   => 'Swimming',
            'value' => get_post_meta(get_the_ID(), 'Swimming',true),
            'compare' => 'IN'
        ),
      array(
            'key'   => 'Boat', 
            'value' => get_post_meta(get_the_ID(), 'Boat',true),
            'compare' => 'IN'
        ))));
        if($posts) { 
             foreach($posts as $post) {
?>
 <?php the_title(); ?> 
    <?php 
    }  
}
else { 
?>  
<?php echo "No Update";
}
?>
3
  • For a meta query your value should just be the value you're looking for - i.e. change the get_post_meta() calls to just Swimming and Boat. See if that helps.
    – WebElaine
    Jun 23, 2022 at 12:44
  • Note that this would be much faster if it was a taxonomy with a swimming and a boat term, meta_query is slow and doesn't scale well as the site gets larger. Post meta tables were designed for fetching data when you already know the post you want, it was never intended for searches like this. That's what taxonomy tables and tax_query is for. If you need to store data put it in post meta, but if you need to search/find/filter/query for data on a post that isn't in its main content/title, put it in a taxonomy
    – Tom J Nowell
    Jun 23, 2022 at 13:37
  • 1
    Also, get_posts is slower than WP_Query as it's uncached, you need to add the suppress_filters parameter and set it to false so that caching plugins and filters can speed it up
    – Tom J Nowell
    Jun 23, 2022 at 13:39

1 Answer 1

1

The problem is that you used OR, specifically the human verbal colloquial understanding, not the logical definition. What you actually meant was AND.

Here's a table demonstrating what happens to A and B for each comparison:

A B AND OR XOR ===
false false
true true
false true
true false
  • OR means if any of the values are true, e.g. if one is false and the other is true
  • XOR means if one of the values is true ( but not both at the same time )
  • AND means if all/both of the values are true, and none are false
  • ==means if both of the values are equivalent e.g. true and "true" are equivalent
  • ===/EQUAL means if both of the values are exactly the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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