[Resolved] Related Posts by Post Tags

Home Forums Support [Resolved] Related Posts by Post Tags

Home Forums Support Related Posts by Post Tags

  • This topic has 28 replies, 4 voices, and was last updated 4 years ago by David.
Viewing 15 posts - 1 through 15 (of 29 total)
  • Author
    Posts
  • #1097591
    raysn

    Hi there,

    when I was searching for a solution how to implement a related posts area in GeneratePress, I found the following topic: https://generatepress.com/forums/topic/displaying-related-posts/

    David suggested to use WP Show Posts (which I also got) together with the code he mentioned in https://gpsites.co/merch/the-single-post/. This is working great for category-related posts.

    But I want to set tags as the related posts basis. So is there a way to modify Davids code snippet somehow that only posts with one or more matching tags are getting displayed instead of posts of the same category?

    I tried to modify the code but didn’t get it work:

    <div class="wpsp-related-posts  grid-container">
    	<h2>Related Posts</h2>
    <?php
    if ( is_single() ) {
        $tags =  get_the_tags();
        $tag = $tags[0];
    } else {
        $tag = get_tag( get_query_var( 'tag' ) );
    }
    
    $tag_slug = $tag->slug;
    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $tag_slug . '"' );
    ?>
    </div>
    #1097882
    David
    Staff
    Customer Support

    Hi there,

    can you try editing your WPSP list and set the Taxonomy to post_tag but do not select any Terms. let me know.

    #1098151
    raysn

    Hi David,

    thanks for your quick response!

    I’ve changed the settings as you suggested, but unfortunately it still doesn’t work as it should, although at least a few posts are displayed now.

    The related posts only refer to the first defined tag and all other tags are getting ignored (so less posts are shown). Do you have any idea how I can include all tags a post is tagged with?

    #1099658
    David
    Staff
    Customer Support

    Try this:

    <div class="wpsp-related-posts  grid-container">
        <h2>Related Posts</h2>
        <?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>
    #1099665
    raysn

    This works fine for me.

    Thank you very much!

    #1099690
    David
    Staff
    Customer Support

    Awesome – that took a little while to figure out.

    #1239654
    Brian

    Hi David
    This works perfectly, but is it possible to take it one step further?
    I would like it to show posts by tag, but if there is no tag, to should show posts from the category πŸ™‚

    #1239723
    David
    Staff
    Customer Support

    Hmmm… try this:

    <div class="wpsp-related-posts  grid-container">
        <h2>Related Posts</h2>
        <?php
    
        if ( is_single() ) {
            // Get the Tags
            $tags =  get_the_tags();
            $tags_list = [];
            foreach ($tags as $tag)
                $tags_list[] = $tag->slug;
            $tags_term  = implode( ', ', $tags_list);
    
            // Get the Categories
            $cats =  get_the_category();
            $cat = $cats[0];
            $cat_term = $cat->slug;
        }
    
        // output Tags or Categories
        if ( has_tag() ) {
            $tax_term = $tags_term;
        } else {
            $tax_term = $cat_term;
        }
        
        // Display List
    
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'tax_term="' . $tax_term . '"' );
    
        ?>
    </div>
    #1239733
    Brian

    That still shows no posts if there’s not a tag πŸ™
    I guess I could make sure I give everything a tag

    #1239820
    David
    Staff
    Customer Support

    Is the post in a Category ?

    #1239889
    Brian

    Yes, all of the posts are in a category.

    #1239891
    Brian

    Dave,
    It works, when I was testing I had the show posts set as tag, sorry about that!

    So it’s all good now, thanks for the help, again πŸ™‚

    #1239904
    Brian

    Sorry, that was a false dawn.

    It now works if a post doesn’t have a tag and correctly shows posts from the category.
    But doesn’t show anything if a post has a tag.

    #1240263
    David
    Staff
    Customer Support

    I edited the code above

    #1240350
    Brian

    It now shows nothing if there is a tag, and shows posts from the category if there’s no tag.

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