Show posts by tag based on title, in custom post type

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

Read More

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();

Related posts

Leave a Reply

1 comment

  1. 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:

    // Get the slug (This is assuming you 
    // have the current post in a $post 
    // variable. Otherwise, load the post 
    // with the post ID like so:
    // $post = get_post( $post_id );
    $slug = $post->post_name;
    
    // Build args array for query, replacing hyphens with nothing on the slug.
    $args = array('tag' => str_replace('-', '', $slug));
    
    // Set wp_query with tag args
    $wp_query = new WP_Query( $args );
    
    // If we get result posts from our query...
    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; ?>
    <?php 
    // Reset wordpress...
    wp_reset_query();
    wp_reset_postdata();