Home Forums ACF PRO Struggle with Taxonomy field query


Struggle with Taxonomy field query

  • I have a CPT for ‘Staff’ and a custom Taxonomy for ‘Department’ with three Terms (only three deparments). I need to be able to display the staff for each department on individual pages in addition to a page that displays ALL staff separated by department, but due to limitation with the Theme they selected (and insist on keeping) I can only use ONE template file for all of those 4 pages.

    SO on each of those pages I added a Custom Field for the Taxonomy Term to be displayed as a checkbox on the page (back-end) – the three department pages have only 1 box checked, for their department’s term, and the ‘all staff’ page has all three terms checked.

    Now I’m writing the query for the Template file and that’s where I’m struggling, if I don’t try to do a tax query to match the Custom Field’s stored term(s) to the employees stored term, I can display the page fine with all employees separated by department, that’s a simple loop that queries the custom post type and orders it by term…’s when I try to pull the stored term(s) for the Page and then compare it to the stored term(s) for the staff that I get no results at all.

    In the settings for the Field Group, I have ‘save terms’ and ‘load terms’ both set to yes, and the the Return Value is Term ID.

    I’m hoping someone can help me troubleshoot my code, which is this:

        while ( have_posts() ) : the_post();
     	<h2 class="main-title"><?php the_title(); ?></h2>
           <div class="entry-content"><?php the_content(); ?></div> //This is some text about the department and its functions, etc. 
    <?php global $post;
    $tax_terms = get_field('related_staff_category');//this is the ACF field attached to each of the affected Pages
    if ($tax_terms) {
      foreach ($tax_terms as $tax_term)  {
        $args=array(   // Here is where I'm trying to query for staff within each department term
          'post_type' => 'staff',
          'post_status' => 'publish',
          'posts_per_page' => -1,
    	 'tax_query' => array(
                'taxonomy' => 'staff_category',
                'field'    => 'term_id',
                'terms'    => $tax_term,
    			'compare' => 'LIKE'
          'meta_key' => 'sort_order',
          'orderby' => 'meta_value_num',
          'order' => 'ASC'    );
        $qry = null;
        $qry = new WP_Query($args);
        if( $qry->have_posts() ) { 

    Below that is the code/html that displays each staff member with their title, image, and bio.

    It *should* pull the term IDs from the data stored in the ACF field for each page (I’ve verified that it is storing the data correctly in a serialized string in each Page’s metadata), then compare to the stored term ID for each Staff member, and display those that match the Page’s term ID(s)…..but it’s not.

    Can anyone see what I’m doing wrong? Thanks in advance for any suggestions!

Viewing 1 post (of 1 total)

The topic ‘Struggle with Taxonomy field query’ is closed to new replies.