Support

Account

Home Forums General Issues Query taxonomies with a acf field

Solved

Query taxonomies with a acf field

  • Hello

    I was trying to create a query to retrieve all taxonomies with a custom field created via the gui in the back-end. I tried to use the wp get_terms and the WP_Term_Query but both of them returned an empty result every time. Upon a inspection of my wordpress database I noticed that the values and the field itself of the taxonomy was not been created in the _termmeta table, which i find very odd since it would be the logical spot that the field information should be stored (at this time I still have not found where the data of the field is stored), but I’m sure is somewhere in the database, since the query with the function get_field('field_name', 'taxonomy_name_term_id', true) manages to retrieve the field value successfully.

    I might be doing something wrong on the creation of the field which causes the field values not been stored in the correct table (although I don’t see how, since the field is displayed in the taxonomies that I want it to).
    Here is the configuration of the field created:

    • field group name : Product category fields
    • fields
    • field label: featured
    • field name: featured-product-category
    • field type: true/false
    • rules
    • show field: Taxonomy Term
    • condition: is equal to
    • value: Product Categories

    The rest of the configurations are set as the default value, so I don’t think is worth mentioning.

    As for the query of the taxonomies itself I did the following :

    $terms = get_terms( 
    		array(
    			'taxonomy' => 'product_cat',
    			'hide_empty' => false,
    			'meta_key' => 'featured-product-category',
    			'meta_value' => '1',
        )
    	);

    I’m pretty sure the function is been called and that the taxonomy exists, as i did the query without the meta information and returned all the taxonomies successfully. I guess I could just loop through all categories checking for the meta value with the get_field function() but I don’t think is the best way to go.

    I’m not sure if this is how the plugin is supposed to work or if i’m doing something wrong so I appreciate any comments about this subject or maybe a better workaround (the correct way to make the query).

    Thank you

  • Term meta will be used in ACF 5.5 which is due to be released, as far as I know, by the end of the year. If this is needed before then see this post https://support.advancedcustomfields.com/forums/topic/how-to-use-wp-term-meta-on-acf-the-easy-way/ for some temporary solutions.

  • Thank you. That post is just what i needed. I’m going to use one of the solutions on that post.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Query taxonomies with a acf field’ is closed to new replies.