skip to Main Content

My Query arguments;

$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'meta_query' => array(
         array(
             'key'      => 'partner',
             'value'    => $partner,
             'compare'  => 'LIKE',
          ),
    ),
 );

If my value for $partner="561"this query also returns Posts where $partner="5614"

Setting 'compare' => '=' or 'compare' =>'INCLUDE' returns nothing.

Where is my misunderstanding?

Note that due to the vagaries of ACF ‘value’ could either be an array or a string. And this also successfully returns a post where $partner= array("561","21423")

at the moment I am testing the results with if ( in_array($partner, get_field('partner')) ) but that’s clumsy.

2

Answers


  1. NOT TESTED, but doesn’t it about the field type ?
    In meta_query the default value of type is ‘CHAR’

    (Doc: https://developer.wordpress.org/reference/classes/wp_meta_query/#usage)

    And by extension, maybe the comparison is not strict.

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key'      => 'partner',
                'value'    => $partner,
                'compare'  => 'LIKE',
                'type' => 'NUMERIC' // <--- add this line
            ),
        ),
    );
    
    Login or Signup to reply.
  2. I think your problem may be stemming from the instances where ‘value’ is an array. From the docs:

    value (string|array) – Custom field value. It can be an array only
    when compare is ‘IN’, ‘NOT IN’, ‘BETWEEN’, or ‘NOT BETWEEN’. You don’t
    have to specify a value when using the ‘EXISTS’ or ‘NOT EXISTS’
    comparisons in WordPress 3.9 and up.

    https://developer.wordpress.org/reference/classes/wp_meta_query/#accepted-arguments

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search