动态元查询 [英] Dynamic meta_query

查看:22
本文介绍了动态元查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里头破了.通过本网站上的帖子,我设法在 WordPress 中创建了一个自定义分类存档页面.现在我正在尝试向它添加动态复选框过滤器,但我似乎无法让 meta_query 工作.

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',
        )
    )
);

然而,这个不会:

$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);

无论我在 URL 中输入什么,它都会不断查找所有结果,并且不会像第一个那样进行过滤.一个 print_r($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 ) )

编辑 07-06-2016//09:00在阅读了说明我应该使用IN"的评论后,我进行了进一步的试验,当我将它与查询本身一起使用时,它根本没有给我任何结果.似乎IN"是问题所在.我正在查询的字段是高级自定义字段"字段,因此可能与它有关?但是他们网站上的例子也使用了相同的方法.

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.

不工作:

    $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',
        )
    ),
);

工作:

        $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',
        )
    ),
);

可选地,我可以在 meta_query 中为数组中的每个值创建一个数组条目,但这可能并不理想.

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

推荐答案

在这篇文章的帮助下,问题解决了!值以序列化方式存储,因此需要一些不同的方法.https://wordpress.stackexchange.com/questions/183182/meta-query-比较不工作

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

这篇关于动态元查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆