Support

Account

Home Forums General Issues Help in using Taxonomy Field Type

Solved

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

  • Works perfect! Thanks a lot 🙂

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

The topic ‘Help in using Taxonomy Field Type’ is closed to new replies.