Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress meta_query key array

Tags:

wordpress

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

like image 920
Ciprian Tepes Avatar asked May 13 '11 11:05

Ciprian Tepes


2 Answers

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.

'meta_query' => array(
  array(
    'key' => '_my_meta',
    'value' => 's:12:"addtosidebar";s:2:"on";',
    'compare' => 'LIKE'
  )
)
like image 70
Richard M Avatar answered Nov 06 '22 00:11

Richard M


it was like this: 'meta_query' => array( array( 'key' => '_my_meta', 'value' => 'addtosidebar', 'compare' => 'LIKE' ) ) – CIPPO Design

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:

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

Post 2:

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

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:

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

Hope that will help someone and that I manage to make sense.

ps: Sorry I'm not hi-profile enough to just comment.

like image 26
Mighty Mess Avatar answered Nov 05 '22 23:11

Mighty Mess