On my website I have a directory (custom post type) filled with companies, each have their own listings page. Now at the bottom of the page I want to display all posts which are tagged with the title of that company as shown in the their directory listing page.
Directory Page
Example Company
Related posts for Example Company
For testing, I manually put ‘rennicks’ as the tag. Then added ‘rennicks’ as a tag in about 5 posts, and they all displayed in the listings page fine. But obviously I need it to dynamically retrieve the title and search for tag based on the data in that variable.
$original_query = $wp_query;
$wp_query = null;
$args=array('tag' => 'rennicks');
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
?>
<?php
while (have_posts()) : the_post(); ?>
<div class="small-12 medium-6 columns content-excerpt">
<div class="thumbnail medium-6 columns nopm">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<div class="content">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<p><?php //the_excerpt(); ?></p>
<div class="related-stats-info">
<ul>
<!-- <li><?php //the_author(); ?></li> -->
<li><i class="fa fa-clock-o"></i> <?php the_date('Y-m-d') ?></li>
<li><i class="fa fa-comments"></i> <?php comments_number( '0 comments', '1 comment', '% comments' ); ?></li>
</ul>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clearboth"></div>
<?php
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
You’ll need to get the post slug, so first I would read that comment and verify that you’re getting the slug. Try maybe doing a var_dump($slug); exit; right after the slug variable to verify you have its correct value. Once you do, try giving this a shot: