I’ve a postmeta key saved in DB: mediSHOP_product_extras
It’s value is
Unserialized: Array ( [0] => Array ( [is_slider] => 0 [is_featured] => 0 [in_stock] => 0 [video_url] => [related_product_list] => ) )
For simplicity I didn’t want to save each value as a new key/value combination in DB. But I might have made a mistake…
I want to use get_posts to fetch posts that have is_featured == 1
And I cannot figure out how to do this…
$slides_content = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 12,
'meta_key' => 'mediSHOP_product_extras',
'meta_value' => '1'
));
meta value needs to be 1, but it also needs to be is_featured == 1
.
Simplest solution would be to just save is_featured as a separate key. But maybe you know how to solve my problem?
2 Answers
It is possible if you shape your meta query to look for the serialized string like this, assuming your array value is always an integer:
$params = array(
'meta_query' => array(
array(
'key' => 'mediSHOP_product_extras',
'value' => 's:11:"is_featured";i:1;',
'compare' => 'LIKE'
)
)
);
$query = new \WP_Query( $params );
If the array value is a string or a boolean, the meta query value pattern should be altered:
// for string type values
's:11:"is_featured";s:1:"1";'
// for bool type values
's:11:"is_featured";b:1;'