简体   繁体   English

尝试通过自定义分类术语过滤 Wordpress 查询

[英]Trying to filter Wordpress query by custom taxonomy terms

I'm trying to filter a Wordpress query based on terms in a custom taxonomy.我正在尝试根据自定义分类法中的术语过滤 Wordpress 查询。 I'm using the Essential Grid plugin to show posts from a custom post type 'offer'.我正在使用 Essential Grid 插件来显示来自自定义帖子类型“报价”的帖子。

I'd like to grab values from the 'f' URL parameter and then use those values as the terms for filtering the query.我想从 'f' URL 参数中获取值,然后将这些值用作过滤查询的条件。

I'm not sure why my code isn't working.我不确定为什么我的代码不起作用。 When I try to save it, I get a message that it will cause a fatal error.当我尝试保存它时,我收到一条消息,它会导致致命错误。

Can someone with more PHP experience help me out?有更多 PHP 经验的人可以帮助我吗? I've gone through the code multiple times and haven't seen the issue.我已经多次浏览代码,但没有看到问题。 Tested several variations as well.还测试了几种变体。

function eg_mod_query($query, $grid_id){

if($grid_id == 3) {
 $f = $_GET['f'];
    $filters = explode(', ', $f);
$args = array(
    'post_type' => 'offer',
    'tax_query' => array(
        array(
            'taxonomy' => 'offer_taxonomy',
            'field'    => 'slug',
            'terms'    => array($filters),
        ),
    ),
);
$query = new WP_Query( $args );
    }
    return $query;
}
$filters = explode(', ', $f);

Line above is already in an array format.上面的行已经是数组格式。 You should not change that to你不应该把它改成

array($filters).

In args var which changes the format.在改变格式的 args var 中。

I think this should work:我认为这应该有效:

function eg_mod_query($query, $grid_id){

if($grid_id == 3) {

    $f = $_GET['f'];
    $filters = explode(', ', $f);
    $args = array(
        'post_type' => 'offer',
        'tax_query' => array(
            array(
                'taxonomy' => 'offer_taxonomy',
                'field'    => 'slug',
                'terms'    => $filters,
            ),
        ),
    );
    $query = new WP_Query( $args );
}

return $query;

} }

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM