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 posted this on StackOverflow too, not sure if that's allowed - if not let me know and I'll remove one (I don't want to look like I'm spamming). Anyway....

I have a custom post type and have used the Verve Meta Boxes plugin to create some custom meta values for the post type.

The meta box is called 'Subjects' and has a checkbox list of subjects to assign values to it. For example, I have ticked Math, English and Science.

Now I want to perform a WP_Query which will take an array of subjects selected by the user as an array, compare those subjects to those selected for the custom post type, and return posts if any of them match.

Here's the code so far:

$subjects_array = explode("_", $_GET["subjects"]);

$args = array(
            'post_status' => 'publish',
            'post_type' => 'any',
            'meta_query' => array(

         // Not sure what type of meta query to do   

            )
        );


$query = new WP_Query($args);

To cut a long story short, I just need to know if there's a way to perform a meta_query which will compare an array against whatever format the data in the meta box is in?

If this is not possible, I just need to know then I'll look at a different way of doing it, but I'm thinking there's perhaps some meta_query ability I don't know about.

Any help would be much appreciated.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Take a look at the codex for a better understanding of queries of custom fields but it should look something like this:

$subjects_array = explode("_", $_GET["subjects"]);

$args = array(
    'post_status' => 'publish',
    'post_type' => 'any',
    'meta_query' => array(
         array(
            'key' => 'field_name',
            'value' => $subjects_array,
            'compare' => 'IN'
        ) 
    )
);


$query = new WP_Query($args);
share|improve this answer
    
That's exactly what I needed - thank you very much! –  Adam Moss Jul 5 '11 at 8:13
    
Glad i could help –  Bainternet Jul 5 '11 at 9:25

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.