wp query - meta_query in WP_Query value is not accepting array

admin2025-01-07  3

I'm trying to pass an array of values into a meta_query value.

// Works as expected to return posts with 1474 in featured
'meta_query' => array(
                array(
                    'key' => 'featured',  
                    'value' => 1474, 
                    'compare' => 'LIKE' 
                ) 
)   

// Works as expected to return posts with 2213 in featured
'meta_query' => array(
                array(
                    'key' => 'featured',  
                    'value' => 2213, 
                    'compare' => 'LIKE' 
                ) 
)   

However when I set as an array it does not work correctly. I am trying to return posts with 'featured' set to either 1474 or 2213.

// Does not work, wp_query returns all posts
'meta_query' => array(
                array(
                    'key' => 'featured',  
                    'value' => array(1474, 2213), 
                    'compare' => 'LIKE' 
                ) 
)   

I have also changed LIKE to IN with no success. I would like to stay away from multiple meta_query statements as the length of my array may change.

I'm trying to pass an array of values into a meta_query value.

// Works as expected to return posts with 1474 in featured
'meta_query' => array(
                array(
                    'key' => 'featured',  
                    'value' => 1474, 
                    'compare' => 'LIKE' 
                ) 
)   

// Works as expected to return posts with 2213 in featured
'meta_query' => array(
                array(
                    'key' => 'featured',  
                    'value' => 2213, 
                    'compare' => 'LIKE' 
                ) 
)   

However when I set as an array it does not work correctly. I am trying to return posts with 'featured' set to either 1474 or 2213.

// Does not work, wp_query returns all posts
'meta_query' => array(
                array(
                    'key' => 'featured',  
                    'value' => array(1474, 2213), 
                    'compare' => 'LIKE' 
                ) 
)   

I have also changed LIKE to IN with no success. I would like to stay away from multiple meta_query statements as the length of my array may change.

Share Improve this question asked Apr 24, 2020 at 19:55 user3447788user3447788 112 bronze badges 3
  • Does it work using the IN compare? – Howdy_McGee Commented Apr 24, 2020 at 20:23
  • No, using IN returns no posts – user3447788 Commented Apr 24, 2020 at 20:37
  • What is the full meta value for featured? – Jacob Peattie Commented Apr 25, 2020 at 3:04
Add a comment  | 

1 Answer 1

Reset to default 0

I manually added a "featured" meta key to 2 different posts with values of 1474 and 2213 and then ran a basic WP_Query() like so:

$qv = [
    'meta_query' =>[
        [
            'key'     => 'featured',  
            'value'   => [1474, 2213], 
            'compare' => 'IN',      
        ]
    ] 
];
$r = new WP_Query($qv);

It pulled up both posts. This was query it generated:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID 
FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 
WHERE 1=1  
AND ( 
    ( wp_postmeta.meta_key = 'featured' AND wp_postmeta.meta_value IN ('1474','2213') )
) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC 
LIMIT 0, 12

var_dump() out your WP_Query() and see what sql query WP is running.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258099a489.html

最新回复(0)