Skip to main content
updated code formatting, it was all outta whack
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175
<?php
                                                    
                                                    $blogcats = get_the_term_list();
                                                                                                            
                                                    if (( 'post' == get_post_type() )) 
                                                            {
                                                                $posttypenews = 'post';
                                                            }
                                                            
                                                    if (( 'blog' == get_post_type() )) 
                                                            {
                                                                $posttypenews = 'blog';
                                                            }
                                                ?>
                                                <div id="news-related-posts-title" class="clear"><h1>Related Posts</h1></div>
                                                <?php
                                                    
                                                $related = get_posts( array(    
                                                                                'numberposts' => 5,
                                                                                'blog_cats' => $blogcats,
                                                                                'post_type' => $posttypenews,
                                                                                'post__not_in' => array($post->ID) ) );
                                       ) );
        if( $related ) foreach( $related as $post ) {
                                                setup_postdata($post); ?>
                                                    ?>
                                             
        <div id="news-related-posts">
                                                        
                                                        <div id="news-related-posts-container">
                                                            
                                                            <div id="news-related-posts-left">
                                                                <?php  the_post_thumbnail('thumbnail'); ?>
                                                            </div>
                                                            
                                                            <div id="news-related-posts-right">
                                                                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                
                                                <?php }
                                                wp_reset_postdata();  
?>  

If I change $blogcats = get_the_terms();$blogcats = get_the_terms(); to $blogcats = 'the tag_id';$blogcats = 'the tag_id'; is works, but I'm struggling to call it from the post thats loaded...

<?php
                                                    
                                                    $blogcats = get_the_term_list();
                                                                                                            
                                                    if (( 'post' == get_post_type() )) 
                                                            {
                                                                $posttypenews = 'post';
                                                            }
                                                            
                                                    if (( 'blog' == get_post_type() )) 
                                                            {
                                                                $posttypenews = 'blog';
                                                            }
                                                ?>
                                                <div id="news-related-posts-title" class="clear"><h1>Related Posts</h1></div>
                                                <?php
                                                    
                                                $related = get_posts( array(    
                                                                                'numberposts' => 5,
                                                                                'blog_cats' => $blogcats,
                                                                                'post_type' => $posttypenews,
                                                                                'post__not_in' => array($post->ID) ) );
                                                if( $related ) foreach( $related as $post ) {
                                                setup_postdata($post); ?>
                                                    
                                                    <div id="news-related-posts">
                                                        
                                                        <div id="news-related-posts-container">
                                                            
                                                            <div id="news-related-posts-left">
                                                                <?php  the_post_thumbnail('thumbnail'); ?>
                                                            </div>
                                                            
                                                            <div id="news-related-posts-right">
                                                                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                
                                                <?php }
                                                wp_reset_postdata(); ?> 

If I change $blogcats = get_the_terms(); to $blogcats = 'the tag_id'; is works, but I'm struggling to call it from the post thats loaded...

<?php                                               
    $blogcats = get_the_term_list();
                                                            
    if (( 'post' == get_post_type() )) 
    {
        $posttypenews = 'post';
    }
            
    if (( 'blog' == get_post_type() )) 
    {
        $posttypenews = 'blog';
    }
?>
    <div id="news-related-posts-title" class="clear"><h1>Related Posts</h1></div>
      <?php
        $related = get_posts( array(    
            'numberposts' => 5,
            'blog_cats' => $blogcats,
            'post_type' => $posttypenews,
            'post__not_in' => array($post->ID)
        ) );
        if( $related ) foreach( $related as $post ) {
            setup_postdata($post);
      ?>
         
        <div id="news-related-posts">
            
            <div id="news-related-posts-container">
                
                <div id="news-related-posts-left">
                    <?php  the_post_thumbnail('thumbnail'); ?>
                </div>
                
                <div id="news-related-posts-right">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                </div>
            </div>
        </div>
    
    <?php }
    wp_reset_postdata();  
?>  

If I change $blogcats = get_the_terms(); to $blogcats = 'the tag_id'; is works, but I'm struggling to call it from the post thats loaded...

Source Link
user3193817
  • 73
  • 1
  • 1
  • 5

Calling related posts to a custom post type, taxonomy & Tag id

I'm trying to call the related posts of a custom post type by the category of the post that is loaded on the page.

Below is where I am at -

<?php
                                                    
                                                    $blogcats = get_the_term_list();
                                                                                                            
                                                    if (( 'post' == get_post_type() )) 
                                                            {
                                                                $posttypenews = 'post';
                                                            }
                                                            
                                                    if (( 'blog' == get_post_type() )) 
                                                            {
                                                                $posttypenews = 'blog';
                                                            }
                                                ?>
                                                <div id="news-related-posts-title" class="clear"><h1>Related Posts</h1></div>
                                                <?php
                                                    
                                                $related = get_posts( array(    
                                                                                'numberposts' => 5,
                                                                                'blog_cats' => $blogcats,
                                                                                'post_type' => $posttypenews,
                                                                                'post__not_in' => array($post->ID) ) );
                                                if( $related ) foreach( $related as $post ) {
                                                setup_postdata($post); ?>
                                                    
                                                    <div id="news-related-posts">
                                                        
                                                        <div id="news-related-posts-container">
                                                            
                                                            <div id="news-related-posts-left">
                                                                <?php  the_post_thumbnail('thumbnail'); ?>
                                                            </div>
                                                            
                                                            <div id="news-related-posts-right">
                                                                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                
                                                <?php }
                                                wp_reset_postdata(); ?> 

If I change $blogcats = get_the_terms(); to $blogcats = 'the tag_id'; is works, but I'm struggling to call it from the post thats loaded...

Am I missing something obvious?

Thanks in advance