I am trying to run a meta_query with an array of values and have it search if all are present in the meta value which is stored in a serialized array. Is this possible?
My arguments for the query is as follows (note that this is nested in a class):
$args = array(
'post_type' => $this->posttype,
'posts_per_page' => '9',
'paged' => $paged,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'lumens',
'meta_query' => array(
array(
'key' => 'mount',
'value' => array('pendant' , 'wall'),
'compare' => 'IN'
)
)
);
An example of the meta data stored, is in a serialized array similar to below:
a:4:{i:0;s:7:"pendant";i:1;s:15:"surface-ceiling";i:2;s:4:"wall";i:3;s:14:"aircraft-cable";}
My query will not return the appropriate results no matter what I try. I am realizing now that I probably should have stored each value in a different meta-key rather then in an array, however, there is far to many entries already to change the metadata now.
UPDATE:
This was my workaround, similar to @Leander approach; I did not want to alter the serialized inputs due to the amount of entries already on the database and there was one thing I forgot to mention, I was utilizing the CMB2 Developer Toolkit which stores checkbox fields as serialized data natively.
// This is pulled from a user input
$meta_queries = array('pendent' , 'wall');
// Metaqueries are passed through loop to create single entries with the like comparison operator
foreach($meta_queries as $key => $value){
$meta_query = array(
'key' => '_tf_' . $key,
// Serialize the comparison value to be more exact
'value' => serialize(strval($value)),
'compare' => 'LIKE',
);
}
// Generate $args array
$args = array(
'post_type' => $this->posttype,
'posts_per_page' => '9',
'paged' => $paged,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'lumens',
'meta_query' => $meta_queries
);
I did not notice much of a performance issue while populating the data. I imagine this approach would have to be reworked if there was an overwhelming amount of data to process.