[Resolved] Code to show related posts based on tags

Home Forums Support Code to show related posts based on tags

  • This topic has 6 replies, 3 voices, and was last updated 8 months ago by Satish Ithamsetty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1176107
    Marcel

    Hi, is there a code available (to place in a hook) to show related posts based on tags? For example: If this post has the tag “trademark” 3 related post with the same tag should be displayed where you now see “Related articles” (in pink) on this page: https://entrepreneurcaribbean.com/2020/02/24/trademarks-as-the-entrepreneurs-value-creating-tool/.

    Currently I’m using the WordPress Popular Posts plugin which makes it possible to do the same but the settings has to be done manually on every post.

    Thanks for your help.

    #1176599
    Leo
    Staff
    Customer Support

    Hi there,

    Unfortunately there is nothing built-in in GP for this.

    Related posts feature is actually quite complicated so best to handled by a plugin.

    I’m not sure how WordPress Popular Posts plugin works but are you able to add it with hooks element so it can be displayed in all posts instead of adding it for each post?

    Let me know 🙂

    #1176625
    Marcel

    Would something like this work?

    <?php
    //for use in the loop, list 5 post titles related to first tag on current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    echo 'Related Posts';
    $first_tag = $tags[0]->term_id;
    $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'posts_per_page'=>5,
    'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
     
    <?php
    endwhile;
    }
    wp_reset_query();
    }
    ?>

    I found it on: https://www.wpbeginner.com/wp-tutorials/how-to-display-related-posts-in-wordpress/

    #1176648
    Leo
    Staff
    Customer Support

    I’m not sure. Feel free to give it a shot.

    #1176675
    Marcel

    I got it! This works:

    <?php
                                    $orig_post = $post;
                                    global $post;
                                    $tags = wp_get_post_tags($post->ID);
                            
                                    if ($tags) {
                                        $tag_ids = array();
                                    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
                                        $args=array(
                                            'tag__in' => $tag_ids,
                                            'post__not_in' => array($post->ID),
                                            'posts_per_page'=>3, // Number of related posts to display.
                                            'caller_get_posts'=>1
                                        );
                            
                                    $my_query = new wp_query( $args );
                            
                                    while( $my_query->have_posts() ) {
                                        $my_query->the_post();
                                ?>
                                <ul class="wpp-list">
                                    <li><a rel="external" href="<? the_permalink()?>"><?php the_title(); ?></a></li>
                                </ul>
                                <?php }
                                    }
                                    $post = $orig_post;
                                    wp_reset_query();
                                ?>
    #1176676
    Leo
    Staff
    Customer Support

    Awesome!

    Thanks for reporting back.

    I’ll keep in mind if someone else asks 🙂

    #1179323
    Satish Ithamsetty

    Hello Marcel,

    Most of the generatepress layouts are not having any related posts feature. The developer removed the code because of speed improvement. He developed a special plugin called “WP Show Posts”. It has the shortcode feature to place the related posts anywhere within your blog by using theme inbuilt “HOOK” features. I am using this plugin for a few months. this plugin does not affect on theme and page loading speed. you can check in my blog post How to Add FAQ Schema.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.