Wordpress meta_query keyword array

I am using the http://www.farinspace.com/how-to-create-custom-wordpress-meta-box/ function to create some custom meta fields.

My WP_Query looks like this:

$args = array( 'post_type' => 'testimonials', 'post_status' => 'publish', 'orderby' => checked((bool) $instance['testimonials_random'], true, false) ? 'rand' : 'id', 'posts_per_page' => $testimonials_number, 'paged' => get_query_var('page'), 'meta_query' => array( array( 'key' => '_my_meta["addtosidebar"]', 'value' => 'on', 'compare' => 'LIKE' ) ) ); $query = new WP_Query($args); 

The addtosidebar input flag looks like this: <input type="checkbox" name="_my_meta[addtosidebar]" <?php checked((bool) $meta['addtosidebar'], true); ?> class="checkbox" /> <input type="checkbox" name="_my_meta[addtosidebar]" <?php checked((bool) $meta['addtosidebar'], true); ?> class="checkbox" />

Do you have any ideas how I can access the key in meta_query?

Thanks Cip

+4
source share
2 answers

I donโ€™t think that you can do this, as in a tutorial related to meta-values, they are stored in an array, which is then serialized into one database field. Thus, you will get something like this in the database: a:4:{s:12:"addtosidebar";s:2:"on";s:3:"foo";s:3:"bar";} .

The following meta request may work, but it would be better to use a separate custom field.

 'meta_query' => array( array( 'key' => '_my_meta', 'value' => 's:12:"addtosidebar";s:2:"on";', 'compare' => 'LIKE' ) ) 
+4
source
 it was like this: 'meta_query' => array( array( 'key' => '_my_meta', 'value' => 'addtosidebar', 'compare' => 'LIKE' ) ) โ€“ CIPPO Design 

This worked for me, I think it needs to be added as an answer. One thing, as Richard pointed out, is that the record in the database is serialized. Thus, "LIKE" will basically just look for the "value" addtosidebar on this line.

For example, if I have a meta array like this:

Message 1:

 $myMeta = array('medium' => 'video', 'sometext' => 'a beautiful video') 

Message 2:

 $myMeta = array ('medium' => 'image', 'sometext' => 'a beautiful image of a video button') 

This means that using the โ€œLIKEโ€ comparison in โ€œvideoโ€ will return both as a video and in the โ€œsometextโ€ value of the second message. To stop this, I had to add quotation marks to limit it:

 $query->set( 'meta_query' , array( array( 'key' => 'blogInfo', 'value' => '"video"', 'compare' => 'LIKE' ) )); 

Hope this helps someone and that I can figure it out.

ps: Sorry, I'm not welcome enough to just comment.

+1
source

All Articles