Home > Mobile >  Wordpress ACF field not working in WP_Query
Wordpress ACF field not working in WP_Query

Time:08-25

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)

CodePudding user response:

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',
        ),
    ),
);

CodePudding user response:

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);
  • Related