I am using the: http://www.farinspace.com/how-to-create-custom-wordpress-meta-box/ function to create some custom meta boxes.
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 input checkbox addtosidebar looks like this:
<input type="checkbox" name="_my_meta[addtosidebar]" <?php checked((bool) $meta['addtosidebar'], true); ?> class="checkbox" />
Do you have any idea how can I access the key in the meta_query?
Thanks,
Cip
I don’t think you can do that as in the tutorial you linked to the meta values are stored in an array, which is then serialised into a single database field. So you end up with something like this in the database:
a:4:{s:12:"addtosidebar";s:2:"on";s:3:"foo";s:3:"bar";}
.The following meta query might work, but it would be better to use a separate custom field.
This worked for me, I think that should be added as an answer. One thing, like Richard pointed out the entry in the database is serialized. So ‘LIKE’ will basically just look for the ‘value’ ‘addtosidebar’ in that string.
For example if I have a meta array like this:
Post 1:
Post 2:
That means that using the compare ‘LIKE’ on ‘video’ will return both as video is also found in the ‘sometext’ value of the second post. To stop that I had to add the quote marks to restrict it:
Hope that will help someone and that I manage to make sense.
ps: Sorry I’m not hi-profile enough to just comment.