简体   繁体   中英

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:-

$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'

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

EDIT: looking at the var dump you posted is appears that Jaguar is a post tag, not a custom taxonomy called 'make'

I managed to sort this in the end, @mrben522 was quite right that the args should be as follows:-

$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. 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. This means that all the code will work with or without ACF.

Posting this solution as it may be helpful to someone else, thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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