Site logo

Archived topic

Related posts with query loop

11 replies · Started by _blank on June 27, 2022

Viewing posts 1–12 of 12

Hello,

I'm currently using WP Show Posts with the generate_after_content hook below to display 5 related posts underneath articles only, no images, no author, no date, no categories and randomly by TAG.

How can I do the same thing without WP Show Posts, please?

<div class="wpsp-related-posts  grid-container">
	<h3>Other tutorials that might interest you...</h3>
<?php
if ( is_single() ) {
        $tags =  get_the_tags();
        $tags_list = [];
        foreach ($tags as $tag)
            $tags_list[] = $tag->slug;
        $tag_string = implode( ', ', $tags_list);
    } else {
        $tag_string = get_tag( get_query_var( 'tag' ) );
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' );
    ?>
</div>

Thanks.

Hi there,

are you wanting each of the Post Titles to be a H3?

Hello,

No just add the H3 title from the previous WP Show Posts code that I currently have.

<div class="wpsp-related-posts  grid-container">
	<h3>Other tutorials that might interest you...</h3>
<?php
if ( is_single() ) {
        $tags =  get_the_tags();
        $tags_list = [];
        foreach ($tags as $tag)
            $tags_list[] = $tag->slug;
        $tag_string = implode( ', ', $tags_list);
    } else {
        $tag_string = get_tag( get_query_var( 'tag' ) );
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' );
    ?>
</div>

Try this PHP snippet:

function db_related_posts() {
    
    if ( ( is_single() && 'post' == get_post_type() ) ) {

        global $post;

        // Get the tags
        $tags = array();
        $posttags = get_the_tags();
        if ($posttags) {
            foreach($posttags as $tag) {
                $tags[] = $tag->term_id;
            }
        }
        // Set query args
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => '3',
            'orderby' => 'rand',
            'post__not_in' => array( $post->ID ), // don't display current post
            'tag__and' => $tags,         
        );

        $latest = new WP_Query($args);
        // Output loop
        if ( $latest->have_posts() ) {
            echo '<h3>Other tutorials that might interest you...</h3>';
            echo '<ul class="related_post_list">';     
            while ($latest->have_posts()) : $latest->the_post();
            ?>
                <li class="related-post">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </li>
            <?php
            endwhile;
            echo '</li>';
            wp_reset_postdata();
        }
    }
}

add_action('generate_after_content', 'db_related_posts');

Then you can use some CSS to remove the bullets:

.related_post_list {
    list-style-type: none;
    margin-left: 0;
}

Perfect, thanks David 😀

But it displays on WooCommerce product pages, how to display them only under the blog posts just?

Updated the code above :)

Great, thanks a lot David 👌

You're welcome

Hi, I am looking to do sth like this ->

Background - Posts under the category "Country" that describe a country and have "country names" as one of the tags. There are other posts, under other categories, for example, best travel locations, they also have the same country name as tags. For example, Best travel Locations in Brazil will have "brazil" as tag.

Need: Is there a way to create a related post block using GB which I hook at end of *all posts of the category "Country"* and they automatically show posts from other categories which have tag which match the tag of the post. For example, under the profile of brazil, it shows, travel in brazil, food in brazil, places to visit in brazil etc. dynamically.

This archived topic is closed to new replies.