Support

Account

Home Forums General Issues Taxonomy Relationship

Solved

Taxonomy Relationship

  • I have a custom post type for “Speakers”, which has it’s own category “Years”. When editing a page, i want the user to select from the Years, and for the front-end to output the Year name, along with a list of all the Speakers assigned to that year.

    I know the Relationship field can provide a list of all the Speakers, and let a user choose X many Speakers for front-end display, but it would be simpler in this case if they could just choose the Year, since they’ve already attended to categorisation.

    I’m wondering if there’s a tutorial for outputting the Taxonomy field type and if this is what i would use to achieve my goal, or am i heading in the wrong direction?

    I realise standard archive templates and such can be used for this but also want to use the Flexible Content field so that a Speaker category can be displayed in between other elements on any given page.

    • Elliot

    • December 5, 2013 at 9:12 am

    Hi @kurdt_the_goat

    Just to clarify, taxonomies are a core part of WordPress, not of ACF.
    Without ACF, you can create post types and taxonomies, relate them together and also render / query result on the front end.

    In light of this, I can’t be of much help as this is a support forum for the ACF plugin.

    I can however direct you to the WP website where you will find lots of docs outlining how to load a post’s taxonomy terms and how to query posts which have a taxonomy term selected:

    Get the terms from a post:
    http://codex.wordpress.org/Function_Reference/get_the_terms

    WP_Query taxonomy params:
    http://stackoverflow.com/questions/12250957/wordpress-query-posts-taxonomy

    Good luck

    Thanks
    E

  • Thanks Elliot but i still think my query is related to ACF.
    Your documentation doesn’t list the “Taxonomy” field type that one can select when creating field groups in ACF (under the “Relational” heading in the select box).

    I was just looking for a starter tuturial (like your others) on how to output that field’s data, since the possibility exists for a user to choose single or multiple taxonomies when presented with this field on an edit screen.

    And also, once i’ve got the taxonomies and listed their posts, whether or not i’d be able to access all custom fields of those posts (like you can do with a Relationship field type).

    • Elliot

    • December 8, 2013 at 8:46 am

    Hi @kurdt_the_goat

    Thanks for the follow up. I’ll add in the taxonomy field docs soon, but for the time being, you can see the data available with some simple debugging:

    
    <?php 
    
    $taxonomies = get_field('field_name');
    echo '<pre>';
    	print_r( $taxonomies );
    echo '</pre>';
    die;
    
     ?>
    

    The above example shows how you can load the taxonomy field values (when viewing a speaker post), and then simple output them visually so you can see the available data.

    To then get all ‘related’ speakers from that year, you can query the posts like so:

    
    <?php 
    
    $taxonomies = get_field('field_name');
    
    // args
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'speaker',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'year',
    			'field' => 'slug',
    			'terms' => array()
    		),
    	)
    );
    
    if( $taxonomies )
    {
    	foreach( $taxonomies as $term )
    	{
    		$args['tax_query']['terms'][] = $term->slug;
    	}
    }
    
     
    // get results
    $the_query = new WP_Query( $args );
    
     ?>
    

    Please note that the above is not tested, and I am assuming that the variable $term has a property called slug.

    Please debug the code to make sure the data is correct.

    Hope that helps.

    thanks
    E

  • Thanks Elliot, your code gave me a good head start, my end code is a bit different but does what i want!

    In the end i changed Years to a Country taxonomy. Basic setup was as follows:
    Custom Post Type: Speakers
    Taxonomy: Country (attached to Speakers)
    ACF Speakers Field Group with 2 fields:
    – speaker_title
    – speaker_photo
    ACF Page Field Group with 1 Flexible Content field with 2 layouts:
    – basic_wysiwyg (1 wysiwyg field)
    – speakers_country_list (1 taxonomy field to choose multiple countries, set to return as Term Object)

    The following code outputs it all with Countries as headings before each list of Speakers within that country:

    <?php 
    /*
    *  Loop through a Flexible Content field and display it's content with different views for different layouts
    */
    while(has_sub_field("flexible_content")): ?>
     
    	<?php if(get_row_layout() == "basic_wysiwyg"): // layout: Basic Wysiwyg ?>
     
    		<div style="border: 1px solid red; margin: 20px; padding: 20px;">
    			<?php the_sub_field("wysiywg"); ?>
    		</div>
     
    	<?php elseif(get_row_layout() == "speakers_country_list"): // layout: Speaker Country List ?> 
    	
    		<div style="border: 1px solid green; margin: 20px; padding: 20px;">
    			<?php
    				$taxterms = get_sub_field("country"); ?>				
    				<?php foreach ($taxterms as $taxterm) : ?>					
    					<?php
    					$args = array(
    						'post_type' => 'speakers',
    						'tax_query' => array(
    							array(
    								'taxonomy' => 'speakers_country',
    								'field' => 'id',
    								'terms' => $taxterm->term_id
    							)
    						)
    					);
    		
    					$myquery = new WP_Query( $args );
    					if($myquery->have_posts()) : ?>
    						<h2>People in <?php echo $taxterm->name; ?></h2>
    						<ul>
    							<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
    								<li style="margin-bottom: 15px;">
    								<?php 
    								if (get_field('speaker_photo')) {
    									$attachment_id = get_field('speaker_photo');
    									$size = "thumbnail";
    									echo wp_get_attachment_image( $attachment_id, $size );
    									echo "<br />";
    								}
    								?>	
    								<strong><?php the_title(); ?></strong><br />
    								<?php the_field("speaker_title"); ?></li>
    							<?php endwhile; ?>
    						</ul>
    					<?php endif; ?>
    					<?php wp_reset_query(); ?>
    				<?php endforeach; ?>
    		</div> 
    	<?php endif; ?> 
    <?php endwhile; ?>
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.