0

I have posts of custom taxonomy called Areas with terms such as Articles, Presentations, News etc. These terms are applied to the normal post-type "post".

Under a custom-post-type called "Company", I added an Advanced Custom Field Post Object with the field name associated_items to select posts related to this Company.

Currently I have this function on my single-company.php which shows the posts selected in ACF field associated_items...

<?php $post_objects = get_field('associated_items');?>
<?php if( $post_objects ): ?>
        <div class="relatedposts">
            <h3>Related Posts</h3>
            <?php foreach( $post_objects as $post): ?>
            <?php setup_postdata($post); ?>
                    <?php get_template_part( 'content', 'excerpt' ); ?>
            <?php endforeach; ?>
        </div><!-- div.relatedposts -->
    <?php wp_reset_postdata(); ?>
<?php endif; // end if( $post_objects ) ?>

The above code only outputs the posts selected in the ACF custom field associated_items but in chronological order whereas I'd like it to be grouped according to taxonomy terms, for example like the following:

Posts related to the Company

Articles

  • a post under the area "Articles"
  • another post under the area "Articles"
  • and another post under the area "Articles"

Presentations

  • a post under the area "Presentations"
  • another post under the area "Presentations"
  • and another post under the area "Presentations"

News

  • a post under the area "News"
  • another post under the area "News"
  • and another post under the area "News"

Many many many thanks in advance!

1 Answer 1

2

I'd recommend creating 3 separate ['associated_items'] fields instead of 1.

For Example:

  • ['associated_articles'] & "Filter for taxonomy term: Articles"
  • ['associated_presentations'] & "Filter for taxonomy term: Presentations"
  • ['associated_news'] & "Filter for taxonomy term: News"

Note: Filtering for the taxonomy term, limits the related posts available to choose from as well, which results in a better user experience

Then change the code above to be used for each field.
For Example:

<h2>Posts related to the Company</h2>
<?php $assoc_articles = get_field('associated_articles');?>
<?php if( $assoc_articles ): ?>
    <div class="relatedposts">
        <h3>Articles</h3>
        <?php foreach( $ass_articles as $article): ?>
        <?php setup_postdata($article); ?>
                <?php get_template_part( 'content', 'excerpt' ); ?>
        <?php endforeach; ?>
    </div><!-- div.relatedposts -->
<?php wp_reset_postdata(); ?>
<?php $assoc_presentations = get_field('associated_presentations');?>
<?php if( $assoc_presentations ): ?>
    <div class="relatedposts">
        <h3>Presentations</h3>
        <?php foreach( $assoc_presentations as $presentation ): ?>
        <?php setup_postdata($presentation ); ?>
                <?php get_template_part( 'content', 'excerpt' ); ?>
        <?php endforeach; ?>
    </div><!-- div.relatedposts -->
<?php wp_reset_postdata(); ?>
<?php $assoc_news = get_field('associated_news');?>
<?php if( $assoc_news ): ?>
    <div class="relatedposts">
        <h3>News</h3>
        <?php foreach( $assoc_news as $news_item ): ?>
        <?php setup_postdata($news_item ); ?>
                <?php get_template_part( 'content', 'excerpt' ); ?>
        <?php endforeach; ?>
    </div><!-- div.relatedposts -->
<?php wp_reset_postdata(); ?>

I'm assuming you are using the above code sample because you prefer it. However, if all you need is a link to the post, you don't actually need to setup_postdata()... you could simply use the $post->ID and then just loop through the results using something like:

<?php foreach( $assoc_news as $news_item ): 
$news_ID = $news_item->ID; ?>
<li><a href="<?php get_permalink($news_ID ); ?>"><?php get_the_title($news_ID ); ?></a></li>... etc

I hope that helps.

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.