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 series of day slots per location, in a multicheckbox custom field. I have a search page where the user can select location, weekday desired, and activity type. Right now I just have 2 samples: a class that meets on Tuesdays and one that does not. When searching for a Tuesday class, however, both are showing up. Here is how I build my argument:

$searchday = $day; // save this value for the results_array
                        $day = array();
                        for($a=1; $a<=10; $a++) {
                            $day[$a] = array(
                                'key' => '_cmb_'.$searchday.'_location_'.$a,
                                'value' => $location,
                                'compare' => 'LIKE'
                                );
                        }

$args = array('posts_per_page' => -1,
                        'post_type' => $class_type,
                        'meta_query' => array(
                                                'relation' => 'OR',
                                                (($day) ? $day : ''),

                                             ),
                        );

And here is that output as print_r

Array
(
[posts_per_page] => -1
[post_type] => aquatics
[meta_query] => Array
    (
        [relation] => OR
        [0] => Array
            (
                [1] => Array
                    (
                        [key] => _cmb_tuesday_location_1
                        [value] => huntley
                        [compare] => LIKE
                    )

                [2] => Array
                    (
                        [key] => _cmb_tuesday_location_2
                        [value] => huntley
                        [compare] => LIKE
                    )

                [3] => Array
                    (
                        [key] => _cmb_tuesday_location_3
                        [value] => huntley
                        [compare] => LIKE
                    )

                [4] => Array
                    (
                        [key] => _cmb_tuesday_location_4
                        [value] => huntley
                        [compare] => LIKE
                    )

                [5] => Array
                    (
                        [key] => _cmb_tuesday_location_5
                        [value] => huntley
                        [compare] => LIKE
                    )

                [6] => Array
                    (
                        [key] => _cmb_tuesday_location_6
                        [value] => huntley
                        [compare] => LIKE
                    )

                [7] => Array
                    (
                        [key] => _cmb_tuesday_location_7
                        [value] => huntley
                        [compare] => LIKE
                    )

                [8] => Array
                    (
                        [key] => _cmb_tuesday_location_8
                        [value] => huntley
                        [compare] => LIKE
                    )

                [9] => Array
                    (
                        [key] => _cmb_tuesday_location_9
                        [value] => huntley
                        [compare] => LIKE
                    )

                [10] => Array
                    (
                        [key] => _cmb_tuesday_location_10
                        [value] => huntley
                        [compare] => LIKE
                    )

            )

    )

)

Not sure what I am doing wrong here. I went so far as to examine the database to ensure that this class (which should not show up) holds no value for meta_key '_cmb_tuesday_location_1' .. 2, 3, etc - through 10. No key should mean huntley value is not found. Thanks for reading.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

i'm not sure about this one, but i think you have too many nested arrays.

you should be getting something like

[meta_query] => Array
(
    [relation] => OR
    [0] => Array
                (
                    [key] => _cmb_tuesday_location_1
                    [value] => huntley
                    [compare] => LIKE
                )

    [1] => Array
                (
                    [key] => _cmb_tuesday_location_2
                    [value] => huntley
                    [compare] => LIKE
                )

you can add the conditions directly to the right array:

                    [...]
                    $metaq = array();
                    $metaq[relation] = 'OR'
                    for($a=1; $a<=10; $a++) {
                        $metaq[$a] = array(
                            'key' => '_cmb_'.$searchday.'_location_'.$a,
                            'value' => $location,
                            'compare' => 'LIKE'
                            );
                    }

and then

$args = array('posts_per_page' => -1,
          'post_type' => $class_type,
          'meta_query' => $metaq
);

note: i haven't tested this, sorry

share|improve this answer
    
You're right I think. But how do I get that? I really need to check against all of these fields, so I made $day ben array which itself carries lots of arrays. Is there a cleaner way to include that array of arrays in the argument's meta_query without having to write out $day[1].$day[2]... etc? Can't really do a foreach inside this variable definition. –  GhostToast Aug 8 '12 at 18:33
    
you can use your array directly to the meta_query . i've updated the response for this –  peteroak Aug 8 '12 at 19:18
    
This generates a very good looking $args but the page is now stuck in an infinite loop of loading. Never gets to the result. If I leave off the $day['relation'] = 'OR'; part, it displays no results (because AND is default). So this seems to be what is breaking it, despite it appearing to look correct in my print_r –  GhostToast Aug 8 '12 at 19:42
    
It works if I choose 'AND' (shows no results). I think my query might be too expensive because it is checking all of these values. Might have to research another method. Thank you for your help. –  GhostToast Aug 8 '12 at 20:27
    
hm, i wonder.. do you mind posting the output of $tuesdays = new WP_Query($args); echo "tuesday query: {$tuesdays->request}"; –  peteroak Aug 8 '12 at 21:35
show 1 more 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.