Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a metafield, that contains a series of options and writes the results to an array in a single meta field:

array([0]=>'First',
      [1]=>'Second',
      [2]=>'Third',
     );

I find posts that contain any of the array elements, and display them using WP_Query

$search = new WP_Query('meta_key=mykey&meta_value=second');

But this doesn't seem to work. Any idea on how to achieve this?


I have edited my method, following this question:

$arg['meta_key'] == $key;
$args['meta_value'] = "%|".$value."|%";
$custom_query = new WP_Query($args);

But no darts.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

I believe you have to use meta compare LIKE. If you do a regular meta query, the resulting SQL will be meta_value = your_value, which is never going to find anything because it's trying to match the entire contents of the field.

share|improve this answer
add comment

I'm pretty sure to return or search an array for values (or keys) you need to use meta_query

So something like:

$search = array(
           'post_type' => 'post',
           'meta_query' => array(
                                array(
                                   'key'     => 'mykey',
                                   'value'   => 'second',
                                     )
                                )
                );

 $query = new WP_Query($search);
share|improve this answer
    
No joy I am afraid. –  Mild Fuzz Aug 17 '11 at 15:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.