简体   繁体   English

ACF WP_Query按分类字段过滤

[英]ACF WP_Query Filter by Taxonomy Field

I am trying to filter a CPT to display fields by Taxonomy, I am currently using the following code:- 我正在尝试过滤CPT以按分类显示字段,我目前正在使用以下代码:-

$args = array(
    'posts_per_page'=> -1,
    'post_type'     => 'episode',
    'order'             => 'DESC',
    //'meta_key'        =>  $filter_key,
    //'meta_value'  =>  $filter,                                
    'tax_query' => array(
        array(
            'taxonomy' => 'name',
            'field' => 'make',
            'terms' => array('Jaguar')
        )
    ),  
);

However, this is not returning any results. 但是,这不会返回任何结果。

This is what I am trying to filter:- 这就是我要过滤的内容:-

array(3) { [0]=> object(WP_Term)#7336 (10) { ["term_id"]=> int(25) ["name"]=> string(6) "Jaguar" ["slug"]=> string(6) "jaguar" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(25) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [1]=> object(WP_Term)#7493 (10) { ["term_id"]=> int(24) ["name"]=> string(13) "Mercedes K100" ["slug"]=> string(13) "mercedes-k100" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(24) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [2]=> object(WP_Term)#7492 (10) { ["term_id"]=> int(26) ["name"]=> string(10) "Porche 911" ["slug"]=> string(10) "porche-911" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(26) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } } array(3) { [0]=> object(WP_Term)#7503 (10) { ["term_id"]=> int(25) ["name"]=> string(6) "Jaguar" ["slug"]=> string(6) "jaguar" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(25) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [1]=> object(WP_Term)#7490 (10) { ["term_id"]=> int(24) ["name"]=> string(13) "Mercedes K100" ["slug"]=> string(13) "mercedes-k100" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(24) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } [2]=> object(WP_Term)#7489 (10) { ["term_id"]=> int(26) ["name"]=> string(10) "Porche 911" ["slug"]=> string(10) "porche-911" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(26) ["taxonomy"]=> string(8) "post_tag" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } }

So the name of my taxonomy field is called 'make' and for test purposes I just want to display all the posts that have a Taxonomy of 'Jaguar' 因此,我的分类法字段的名称称为“ make”,出于测试目的,我只想显示所有具有“ Jaguar”分类法的帖子

Please advise. 请指教。

array(
            'taxonomy' => 'name',
            'field' => 'make',
            'terms' => array('Jaguar')
        )

is wrong. 是错的。 should be 应该

array(
            'taxonomy' => 'post_tag',
            'field' => 'name',
            'terms' => array('Jaguar')
        )

Check out the docs on WP Query taxonomy parameters for info on how that works 查看有关WP Query分类标准参数的文档,以获取有关其工作原理的信息

EDIT: looking at the var dump you posted is appears that Jaguar is a post tag, not a custom taxonomy called 'make' 编辑:查看您发布的var dump,看来Jaguar是发布标记,而不是称为“ make”的自定义分类法

I managed to sort this in the end, @mrben522 was quite right that the args should be as follows:- 我设法将其最终排序,@ mrben522是​​正确的,参数应该如下:

$args = array(
    'posts_per_page'=> -1,
    'post_type'     => 'episode',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_tag',
            'terms' => array('Jaguar'),
            'field' => 'name',              
        )
    ),
    'order'             => 'DESC',                                    
);

However, this alone didn't resolve the issue. 但是,仅此一项并不能解决问题。 What I also had to do is change the option for the Taxonomy field within ACF. 我还要做的是更改ACF中“分类”字段的选项。 The 'Save Terms' option was set to 'No' but changing this option to 'Yes' got this working for me. “保存条款”选项设置为“否”,但是将此选项更改为“是”可以使我满意。

在此处输入图片说明

If you are using the 'Save Terms' setting in the taxonomy field, the selected terms will be saved as connections between the post and the term - just like WP core. 如果您在分类法字段中使用“保存条款”设置,则所选条款将被保存为帖子和该条款之间的连接-就像WP核心一样。 This means that all the code will work with or without ACF. 这意味着所有代码都可以使用或不使用ACF。

Posting this solution as it may be helpful to someone else, thanks! 发布此解决方案,因为它可能对其他人有帮助,谢谢!

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

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