Dynamic meta_query

2019-09-10 11:38发布

I'm breaking my head over here. Through a post on this website I've managed to create a custom taxonomy archive Page in WordPress. Now I'm trying to add dynamic checkbox filters to it, but I can't seem to get the meta_query working.

This line of code works like I would like it to work;

    $query = array(
    'post_type' => 'company',
    'posts_per_page' => 999,
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'company_category',
            'field' => 'slug',
            'terms' => $al_cat_slug
        )
    ),
    'meta_query' => array (
        array (
            'key' => 'company_method',
            'value' => 'Online',
            'compare'   => 'LIKE',
        )
    )
);

How ever, this one won't:

$query = array(
    'post_type' => 'company',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'company_category',
            'field' => 'slug',
            'terms' => $al_cat_slug
        )
    ),
);

$al_tax_post_qry = new WP_Query($query);

$meta_query = $al_tax_post_qry->get('meta_query');

$name = 'company_method';
$value = explode(',', $_GET[ $name ]);

$meta_query[] = array(
        'key'       => $name,
        'value'     => $value,
        'compare'   => 'LIKE',
);

$al_tax_post_qry->set('meta_query', $meta_query);

Whatever I enter in the URL, it keeps finding all the results and it won't filter like the first. A print_r($meta_query); gives me:

Array ( [0] => Array ( [key] => company_method [value] => Array ( [0] => Online ) [compare] => LIKE ) )

Edit 07-06-2016 // 09:00 After reading the comment stating that I should use 'IN', I've experimented a little further and when I use it withing the query itself, it gives me no results at all. It seems 'IN' is the issue. The field I'm querying is an 'Advanced Custom Fields' field so that might have something to do with it? However the examples on their website also use the same method.

Not Working:

    $query = array(
    'post_type' => 'company',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'company_category',
            'field' => 'slug',
            'terms' => $al_cat_slug
        )
    ),
    'meta_query' => array (
        'relation'      => 'AND',
        array (
            'key' => 'company_method',
            'value' => array('online', 'orange', 'apple'),
            'compare'   => 'IN',
        )
    ),
);

Working:

        $query = array(
    'post_type' => 'company',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'company_category',
            'field' => 'slug',
            'terms' => $al_cat_slug
        )
    ),
    'meta_query' => array (
        'relation'      => 'AND',
        array (
            'key' => 'company_method',
            'value' => 'online',
            'compare'   => 'LIKE',
        )
    ),
);

Optionally I could create an array entry in the meta_query for every value in the array, but that might not be ideal.

标签: wordpress
2条回答
走好不送
2楼-- · 2019-09-10 12:12

If you define your $meta_query value as an array, you should change your compare operator. IN and NOT IN are the array specific ones.

Try this:

$meta_query[] = array(
    'key'       => $name,
    'value'     => $value,
    'compare'   => 'IN',
);

Hope it helps!

查看更多
Ridiculous、
3楼-- · 2019-09-10 12:15

With help from this post, the issue is solved! Values are stored in a serialized way, so need a little different approach. https://wordpress.stackexchange.com/questions/183182/meta-query-compare-in-not-working

查看更多
登录 后发表回答