Home › Forums › General Issues › Help in using Taxonomy Field Type
Hi,
I have created a custom field with Field Type as Taxonomy and Taxonomy selected as Categories.
I want to echo the Category selected by the user in the Page which I can use it in my code.
Below is the code in which I want to use the select Category:
<?php $args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => '$value',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true ); ?>
For the variable $value, I want the category to be selected. I am new to custom development and need your help in using this plugin.
I checked out the post http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/ but was not able to implement the same.
I tried something like this $value = get_field_objects('select_category_for_news', 'category_1'); ?>
Thank you,
—
Shirish
Hi @cshirish
To load the selected values from a taxonomy field, you can use the get_field function like so:
$selected = get_field('taxonomy_field_name');
I just looked at the get_posts documentation and the following is very useful:
Note: The category parameter needs to be the ID of the category, and not the category name.
Note: The category parameter can be a comma separated list of categories, as the get_posts() function passes the 'category' parameter directly into WP_Query as 'cat'.
So, with that in mind, please edit your taxonomy field and select ‘ID’ as the return format setting. Then you can use the following code:
$selected = get_field('taxonomy_field_name');
$args = array(
'posts_per_page' => 3,
'offset' => 0,
'category' => implode(',',$selected),
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
Hope that helps.
Thanks
E
The topic ‘Help in using Taxonomy Field Type’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.