Home Forums Front-end Issues Display related posts using specific tag via taxonomy custom field


Display related posts using specific tag via taxonomy custom field

  • Hi,

    I have a CPT template working fine for a destination cpt. I want to display related posts at the bottom of the pages using tags. I enabled post_tag in my taxonomy in functins.php and I am able to add a tag on each of the destination edit page. Then I added a taxonomy field named ‘related_places_by_tag’ in my field group. And I am able to select a tag on the edit post template field. This way, all destination posts with a specific tag, say campsite, will display at the bottom like related posts.

    But I don’t know how to use this field <?php the_field('related_places_by_tag'); ?> while querying the posts in the template to show the tagged posts.

    Using it like this does not work:

    						$posts = get_posts(array(
    							'posts_per_page'	=> -1,
    							'post_type'			=> 'post',
    							'tag'               => <?php the_field('related_places_by_tag'); ?>
    						if( $posts ): ?>
    							<?php foreach( $posts as $post ): 
    								setup_postdata( $post )
    									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    							<?php endforeach; ?>
    							<?php wp_reset_postdata(); ?>
    						<?php endif; ?>
  • I tried using this code which I found here :

    							$tagValue = get_field('related_places_by_tag');
    							'tag_id' => $tagValue,
    							'posts_per_page'=>6 // Number of related posts to display
    							$my_query = new wp_query( $args );
    							while( $my_query->have_posts() ) {
    							<a href="<? the_permalink()?>">
    							<?php the_title(); ?>
    							<?php }  

    Though it does not break the template, it doesn’t work.

  • Hi @him,

    Thanks for the post.

    In your use case, don’t you think it would be easier to use a post object field or a Relationship field instead?

    Here are links to the resource pages: and

  • Thanks James, I had totally forgotten that I had raised the topic here. I have since found the solution and its working perfectly well. Here’s the code I am using for reference:

    <div class="post">
    						$relatedd = get_field('related');
    						<?php if( $relatedd ): ?>
    							<?php foreach( $relatedd as $related ): ?>
    									<div class="post-image related">
    									<?php if (has_post_thumbnail($related->ID) ) : ?>
    									<?php echo get_the_post_thumbnail($related->ID, 'tab-small'); ?>
    									<?php endif; ?>
    									</div><!-- .post-image -->
    									<div class="post-content">
    									<h3><a href="<?php echo get_permalink( $related->ID ); ?>">
    										<?php echo get_the_title( $related->ID ); ?>
    										 <?php /* echo wp_trim_words( get_the_title( $related->ID ), 01, '...' ); // trim the words in a title to first word */ ?> 
    									<?php echo custom_field_excerpt( $related->ID ); ?>
    									</div><!-- .post-content -->
    							<?php endforeach; ?>
    						</div><!-- .post -->

    The above code works in the CPT page template and in addition here is the code for the functions.php:

    function custom_field_excerpt($related_post_id) {
    	global $post;
    	$text = get_field('introduction', $related_post_id );
    	if ( '' != $text ) {
    		$text = strip_shortcodes( $text );
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$excerpt_length = 70; // 20 words
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    	return apply_filters('the_excerpt', $text);

    The above code shortens the text from the Introduction field and uses it in related posts.

    Hope it is useful for anybody looking for similar solution.

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

The topic ‘Display related posts using specific tag via taxonomy custom field’ is closed to new replies.