I have a page template that loops through all existing post categories and outputs 5 associated posts for each category. I want to change this so it only outputs the categories I have selected in an ACF Taxonomy field on a particular page.

I have set up the page and added the ACF Taxonomy field to this, however I can't work out how to get the original loop to only use the taxonomies selected here. What's the best way to achieve this?

I've tried passing the taxonomies via get_field('taxonomy_field_name') into the first foreach, however that doesn't seem to be working

Here's how I'm looping through the categories/posts:

/* Loop through Categories and Display Posts */
    $post_type = 'post';
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
    foreach( $taxonomies as $taxonomy ) :
      $terms = get_terms( $taxonomy );
      foreach( $terms as $term ) :
          $args = array(
                  'post_type' => $post_type,
                  'posts_per_page' => 5,  //show all posts
                  'tax_query' => array(
                          'taxonomy' => $taxonomy,
                          'field' => 'slug',
                          'terms' => $term->slug,
          $posts = new WP_Query($args); ?>
          <div class="category-container-row">
          <?php if( $posts->have_posts() ): ?> 
            <div class="category-title">
                    <?php echo '<a href="' . get_site_url() . '/' . $term->slug . '">' . $term->name . '</a>';?>
            <div class="post-listing-row carousel-container">
            <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
            <article class="single-post">
                  <a href="<?php echo get_permalink($post->ID); ?>"><?php  echo get_the_title(); ?></a>
            <?php endwhile; endif; ?>
      <?php endforeach;
    endforeach; ?>

  • Can you please share the code you already have, and what you've attempted, Commented Jul 3, 2019 at 14:47
  • Apologies, that's added now
    – user108167
    Commented Jul 3, 2019 at 14:52
  • Where do you get data from your ACF field ? we can not see that on the code u just put above. And in the tax_query, you'r passing an object 'taxonomy' => $taxonomy, instead of the name of your taxonomy! 'taxonomy' => $taxonomy->name, Commented Jul 3, 2019 at 15:01

1 Answer 1


The ACF "Taxonomy" field lets you select terms from a taxonomy, not taxonomies themselves. So if you want your selections in that field to be used in your code, you need to:

  1. Remove the get_object_taxonomies() line and the subsequent foreach. You're looping through terms, not Taxonomies,
  2. Make sure the Taxonomy field is set to return Term ID. As the primary key, this is likely faster than querying posts by term slugs.
  3. Replace get_terms() with the get_field() for your custom field. This is what you'll be looping over.
  4. Change your tax_query to use term_id as the field, to match the output of the field as set in #2.

That will leave you with something like this:

$post_type = 'post';
$taxonomy  = 'taxonomy_name';
$terms     = get_field( 'taxonomy_field_name' );

foreach( $terms as $term ) :
    $args = array(
        'post_type'      => $post_type,
        'posts_per_page' => 5,
        'tax_query'      => array(
                'taxonomy' => $taxonomy,
                'field'    => 'term_id',
                'terms'    => $term,

    $posts = new WP_Query( $args );

    // Output posts, etc.

  • Amazing, that's sorted it, thanks for your help
    – user108167
    Commented Jul 3, 2019 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.