skip to Main Content

Why does this return all posts;

$partner = get_query_var('partner');

echo "Partner ID: ". $partner; // Echoes correctly

$args = array(
   'post_type' => 'product',
   'posts_per_page' => -1,
   'meta_query' => array(
    'meta_key'      => 'partner',
    'meta_value'    =>  array($partner), // also $partner (without array)
    'compare' => 'LIKE', // Also 'IN'
      ),
    );

$partner_query = new WP_Query($args);

the ACF field ‘partner’ is an array, the query variable is a string (obv)

3

Answers


  1. Chosen as BEST ANSWER

    For anyone who gets stuck in this rabbit hole.

    My query parameter is a string, ACF stores the IDs as a string if there's one and an array if there's more than one.

    This form of the arguments returns the correct results. (many thanks to Howard E and IronCanTaco)

    $partner = get_query_var('partner'); // string
    
    $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => -1,
                    'meta_query' => array(
                        array(
                        'key'      => 'partner', // Could be string or array
                        'value'    =>  $partner,
                        'compare' => 'LIKE',
                        ),
                    ),
                );
    

  2. Try like this:

    $args = array(
       'post_type' => 'product',
       'posts_per_page' => -1,
       'meta_query' => array(
        'meta_key'      => 'partner',
        'meta_value'    =>  array($partner), // also $partner (without array)
        'compare' => '=', // Also 'IN'
          ),
        );
    
    $partner_query = new WP_Query($args);
    
    Login or Signup to reply.
  3. The correct usage of meta_query is like this:

    The args shouldn’t be meta_key if you’re using meta_query – they should be key, value, etc.

    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'meta_query'     => array(
            array(
                'key'     => 'partner',
                'value'   => array( $partner ),
                'compare' => 'IN',
            ),
        ),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search