advanced custom fields - Loop through ACF taxonomies and output associated posts

I have a page template that loops through all existing post categories and outputs 5 associated posts for each category.

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:

<?php 
/* 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(
                      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">
                <h2>
                    <?php echo '<a href="' . get_site_url() . '/' . $term->slug . '">' . $term->name . '</a>';?>
                </h2>
            </div>
            <div class="post-listing-row carousel-container">
            <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
            <article class="single-post">
                  <h3>
                  <a href="<?php echo get_permalink($post->ID); ?>"><?php  echo get_the_title(); ?></a>
                  </h3>
              </article>
            <?php endwhile; endif; ?>
          </div>
        </div>
      <?php endforeach;
    endforeach; ?>

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:

<?php 
/* 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(
                      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">
                <h2>
                    <?php echo '<a href="' . get_site_url() . '/' . $term->slug . '">' . $term->name . '</a>';?>
                </h2>
            </div>
            <div class="post-listing-row carousel-container">
            <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
            <article class="single-post">
                  <h3>
                  <a href="<?php echo get_permalink($post->ID); ?>"><?php  echo get_the_title(); ?></a>
                  </h3>
              </article>
            <?php endwhile; endif; ?>
          </div>
        </div>
      <?php endforeach;
    endforeach; ?>

Share Improve this question edited Jul 3, 2019 at 14:52 user108167 asked Jul 3, 2019 at 14:42 user108167user108167 11 silver badge4 bronze badges 3
  • Can you please share the code you already have, and what you've attempted, – Jacob Peattie 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, – Rachid Chihabi Commented Jul 3, 2019 at 15:01
Add a comment  | 

1 Answer 1

Reset to default 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(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'term_id',
                'terms'    => $term,
            ),
        ),
    );

    $posts = new WP_Query( $args );

    // Output posts, etc.

endforeach;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742308024a4419331.html

相关推荐

  • advanced custom fields - Loop through ACF taxonomies and output associated posts

    I have a page template that loops through all existing post categories and outputs 5 associated posts for each category.

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信