Support

Account

Home Forums ACF PRO Return Taxonomy Slug using Taxonomy Field

Solving

Return Taxonomy Slug using Taxonomy Field

  • So I’m still in the learning process of this all, so sorry if this seems very obvious. I’m attempting to use a Taxonomy Field to return the slug of a taxonomy and input that result into a query_post.

    Below is the background on what I have and what I’m doing to give more context.

    I have created a custom post type “Team” with a few taxonomies, what I’m calling departments, the two “departments” I have thus far are “South Team” and “North Team”, and there will be more down the road. Now what I’m trying to accomplish is this: When a user (the client) adds a new Location, they will first add a new department, then create a new page. Then using the Taxonomy Field (on the new page) select the department the new page is for. On this template page for departments I will have the following code pulling in the taxonomy/department data:

    <?php
    			  query_posts( array( 
    			  	'post_type' => 'staff', 
    			  	'department' => 'taxonomy_field',
    			    'orderby' => 'menu_order',
    			    'order' => 'ASC',
    			    'posts_per_page' => '-1'
    			  ) );
    			  if ( have_posts() ) : while ( have_posts() ) : the_post();
    			?>

    Below this code, I will display the usual grid of smiling faces. So with all the above in mind how can I get the Taxonomy Field to return the slug and input that into ‘department’ => ‘taxonomy-field’ in the query. I have the Taxonomy Field set up to return the value as Term Object, and I’m using the Select for Field Type, and have the Department selected for Taxonomy.

    Thanks for any help.

  • Is the taxonomy field returning the default Term ID or is it returning a Term Object?

  • I have the taxonomy field setup to return as a Term Object

  • basically, if you want to get all of the posts in a term then you should use WP_Query https://codex.wordpress.org/Class_Reference/WP_Query and the documentation will lead you there https://developer.wordpress.org/reference/functions/query_posts/

    The important bit is the tax_query and the fact that your field is returning an term object. You could query by any of the fields of the returned object.

    If you want to see what the object looks like

    
    $department = get_field('department');
    echo '<pre>'; print_r($depearment'); echo '</pre>';
    

    Here is

    
    <?php 
      
      $department = get_field('department');
      
      $query = array(
        'post_type' => 'staff',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'tax_query' => array(
          array(
            'taxonomy' => 'departments',
            'field' => 'term_id', // not needed, default value
            'operator' => 'IN', // not needed, default value
            'terms' => array($department->term_id)
          )
        )
      );
      $team_query = WP_Query($args);
      if ($team_query->have_posts()) {
        while($team_query->have_posts()) {
          $team_query->the_post();
          // output stuff about this post
        }
        wp_reset_postdata();
      }
    

    more information about querying based on different types of acf fields can be fount here https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

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

The topic ‘Return Taxonomy Slug using Taxonomy Field’ is closed to new replies.