• Resolved Chris Bibbs

    (@chrisbibbs)


    Hi,

    First off, great plugin. Probably the second best plugin in the history of WordPress behind ACF itself.

    So I’ve been trying to use Taxonomy Terms field as a create a drop down in a Flexible Content field. I am set up a configuration where the user can display a feed of posts based upon the selected term (via get_posts() and the ‘tax_query’ parameter). What I am trying to do is when the user selects a taxonomy term, it automatically loads the value into the ‘taxonomy’ and ‘terms’ parameters in the ‘tax_query’ array in the get_posts() function.

    One slight problem: for some reason I cannot access the values (either as an object, name, or id) and I am losing my mind trying to figure out how to get the values.

    Part of the code is as follows:

           $args = array(
    		
    		'order'             => $feed_terms_order,
    		'orderby'           => $feed_terms_orderby,
    		'tax_query'         => array(
    				'relation' => 'or',
    					array(
    						'taxonomy' => '' // from taxonomy term field,
    						'field' => 'term_id',
    						'terms' => '' // from taxonomy term field,
    					),
    				),
    		'posts_per_page'    => $feed_terms_numberposts,
    	);
    • This topic was modified 6 months, 1 week ago by Chris Bibbs.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    First, you have to focus on retrieving the Taxonomy Terms field value, and then adapt it to fit your tax_query.

    Get the value

    You said your Taxonomy Terms is inside a Flexible Content in the admin, which means you have to retrieve it using the have_rows(): the_row() > get_sub_field() logic, as explained in the ACF documentation. A typical usage example is:

    // loop flexible content
    if(have_rows('flexible_content')):
        while(have_rows('flexible_content')) : the_row();
    
            // my layout
            if(get_row_layout() === 'my_layout'):
                
                // taxonomy_terms
                $taxonomy_terms = get_sub_field('taxonomy_terms');
    
                // print
                print_r($taxonomy_terms);
    
            endif;
            
        endwhile;
    endif;

    In your code, I see you have some values with $feed_terms_order, $feed_terms_orderby if these are subfields from a Flexible Content too, you should be retrieving them the same way.

    Use it within tax_query

    You should note that the Taxonomy Terms, when displayed as checkbox/multi-select, allows users to select multiple terms, from different taxonomies. If that’s your case, you’ll have to loop on the $taxonomy_terms values (which will be an array of terms) and construct multiple tax_query, like this:

    // prepare tax_query
    $tax_query = array();
    
    // loop taxonomy terms
    foreach($taxonomy_terms as $term){
        
        // append tax_query for each selected term
        $tax_query[] = array(
            'taxonomy' => $term->taxonomy,
            'field'    => 'term_id',
            'terms'    => $term->term_id,
        ),
        
    }
    
    // get_posts args
    $args = array(
        'posts_per_page' => $feed_terms_numberposts,
        'order'          => $feed_terms_order,
        'orderby'        => $feed_terms_orderby,
        'tax_query'      => array(
            'relation' => 'or',
            $tax_query, // use tax_query
        ),
    );

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter Chris Bibbs

    (@chrisbibbs)

    One last question!

    Should the return format be as an ID or as an object?

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Since you’re constructing your own custom tax_query, you’ll need the $term->taxonomy & $term->term_id to populate it, as you can see in the code posted above. So it’s better to return the Object, as you already have access to these data.

    If you decide to return Term ID only, you’ll have to retrieve each term object with get_term() (see documentation).

    Hope it helps!

    Regards.

    Thread Starter Chris Bibbs

    (@chrisbibbs)

    Thank you very much. You resolved my issue.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Awesome!

    Have a nice day!

    Regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to extract values from Taxonomy Terms field’ is closed to new replies.