0
votes

Ive been trying to figure this out for 2 days now and wondering if someone could give some pointers/guidance...

I am getting a list of provinces from a custom field that is stored as an array:

$userprovince = get_user_field ("s2_province_code");

The output using print_r:

Array ( [0] => MB [1] => NU )

Now I'd like to query_posts with a meta_query using that value against the key='province'. I've taken $userprovince and turned it into a multidimensional array using:

for ($i=0; $i< count($userprovince); $i++)
{
    $count = count($arrays);
    $arrays[$count] = array(
                'key' => 'province',
                'value' => $userprovince[$i],
                'compare' => 'LIKE'
                );
}

Which outputs using print_r:

Array ( [0] => Array ( [key] => province [value] => MB [compare] => LIKE ) [1] => Array ( [key] => province [value] => NU [compare] => LIKE ) )

So I've set up my query as follows:

$args = array(
'post_type'         => 'clientresource',
'post_status'       => 'publish',
'category_name' => 'employment-standards',
'meta_query'        => array(
    'relation'  => 'AND',
        $arrays,
        'relation'  => 'OR',
        array(
        'key'       => 'province',
        'value'     => 'All',
        'compare'   => 'LIKE'
    )
)); query_posts($args);

My problem - it doesn't work. The correct records are not gathered. However if I manually set the provinces like this:

$args = array(
'post_type'         => 'clientresource',
'post_status'       => 'publish',
'category_name' => 'employment-standards',
'meta_query'        => array(
    'relation'  => 'OR',
        array(
        'key'       => 'province',
        'value'     => 'NB',
        'compare'   => 'LIKE'
        ),
        array(
        'key'       => 'province',
        'value'     => 'NU',
        'compare'   => 'LIKE'
    ),
array(
        'key'       => 'province',
        'value'     => 'All',
        'compare'   => 'LIKE')
));query_posts($args);

It works and I get the records expected.

The differences between the 2 meta queries are:

Working:

[meta_query] => Array ( [relation] => OR [0] => Array ( [key] => province [value] => NB [compare] => LIKE ) [1] => Array ( [key] => province [value] => NU [compare] => LIKE ) [2] => Array ( [key] => province [value] => All [compare] => LIKE ) ) )

Not Working:

[meta_query] => Array ( [relation] => OR [0] => Array ( [0] => Array ( [key] => province [value] => MB [compare] => LIKE ) [1] => Array ( [key] => province [value] => NU [compare] => LIKE ) ) [1] => Array ( [key] => province [value] => All [compare] => LIKE ) ) )

What I've been struggling with is how to output my array in meta_query so that it contains the same structure as the working example? I've tried outputting it using var_export but it remains the same. I've also tried using foreach to generate the array in the first place but I still can't get it.

Thank you for reading.

1

1 Answers

0
votes

I noticed the meta key is always province. Why don't you try using an IN operator instead.

The documentation says the following about the parameters for WP_META_QUERY class

/*
$key: Meta key to filter by.
$value: Meta value to filter by.
$compare: MySQL operator used for comparing the $value. 
Accepts '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN'...
*/

It actually defaults to 'IN' when you pass an array as value

Line 953 here. https://developer.wordpress.org/reference/classes/wp_meta_query/

$user_province = array( 'NU', 'MB');

$args = array(
'post_type'         => 'clientresource',
'post_status'       => 'publish',
'category_name' => 'employment-standards',
'meta_query'        => array(
    'relation'  => 'OR',
        array(
        'key'       => 'province',
        'value'     => $user_province,
        'compare'   => 'IN'
        )
));

query_posts($args);

By the way, WordPress also suggests not to use query_posts since it can break the loop. The best idea would be for you to implement this with the pre_get_posts filter.

Another way to do just that would be to create a new instance to the WP_Query class. Something like

$args = array('Use the code above');
$customQuery = new WP_Query( $args );
while( $customQuery->have_posts() ){

    $customQuery->the_post()

}

On further consideration, if you want to use the multidimensional array with the same meta_key try doing something like this instead

First, set the relation for all within the $arrays variable. Then all of the conditions

$arrays = array( array( 'relation' => 'OR' ) );
for ($i=0; $i< count($userprovince); $i++)
{
    $count = count($arrays);
    $arrays[] = array(
                'key' => 'province',
                'value' => $userprovince[$i],
                'compare' => 'LIKE'
                );
}

Finally pass the arrays variable to your arguments

$args = array(
'post_type'         => 'clientresource',
'post_status'       => 'publish',
'category_name' => 'employment-standards',
'meta_query'        => $arrays,
); query_posts($args);

Personally I wouldn't recommend this last procedure since the query may result inefficient.

Let me know if this works for you.