[Resolved] Related Posts by Post Tags

Home Forums Support [Resolved] Related Posts by Post Tags

Home Forums Support Related Posts by Post Tags

Viewing 14 posts - 16 through 29 (of 29 total)
  • Author
    Posts
  • #1240915
    Tom
    Lead Developer
    Lead Developer

    Hey Brian,

    How about 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' ) );
        }
    
        if ( ! $tag_string ) {
            $cats =  get_the_category();
            $cat = $cats[0];
            $tag_string = $cat->slug;
        }
    
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' );
        ?>
    </div>

    Let me know 🙂

    #1241395
    Brian

    Still shows nothing if there is a tag, but shows posts from the category if there’s no tag 🙁

    #1242200
    Tom
    Lead Developer
    Lead Developer

    Weird. If you remove the category part (below), does it display based on tags?:

    if ( ! $tag_string ) {
        $cats =  get_the_category();
        $cat = $cats[0];
        $tag_string = $cat->slug;
    }
    #1242532
    Brian

    If I remove that it shows no posts at all.

    #1242889
    David
    Staff
    Customer Support

    I tested the different code options and they are all doing pretty much the same thing. So using Tom’s latest version

    1. All my posts had only Categories, NO tags. And the related posts worked like the original merch site did showing related categories. Thats correct.

    2. Now i edited two of my posts and gave them both the same tag. For these two posts – the related posts just shows me the other post. Thats correct.

    3. Now the issue. If i add a post that has a unique tag, not shared by any other post, the Related Posts displays Nothing Found. Which is actually correct as we’re not testing if there are more than 1 post with the same tag in our fallback condition.

    Can you check if this applies on your site?

    #1243176
    Brian

    Hi David, This doesn’t apply to my site as I only have tags that have at least a few posts. So to clarify my situation, all posts have a category and most also have a tag. The posts with category and tag are not showing related.

    If you cant duplicate the error then I guess I must have a conflict of some sort. I’m thinking that I could make sure all posts have a tag (which is no-index so shouldn’t cause issues) to overcome this.

    #1243960
    David
    Staff
    Customer Support

    I can only assume there is something else effecting that function on your site. Which makes it fairly impossible for us to help in debugging.

    #1244068
    Brian

    FYI, As a last attempt, I tried this with all plugins switched off except GP Premium, Show Posts, and Simple CSS
    I still got nothing when there was a tag.

    #1244141
    David
    Staff
    Customer Support

    If there are no other functions that could be conflicting then maybe the last thing to check is the WP Show Posts List you have set up. Make sure there are no Taxonomies defined or other changes made in the More Settings.

    #1244340
    Brian

    I’ve tried it with and without taxonomies set and there’s nothing in the More settings apart from excluding current. (I tried that off as well), perhaps I’m missing something obvious. But at this point I think I will go ahead and use tags, making sure I give all posts a tag. Thanks for all the help guys.

    #1244389
    Brian

    So I have one final question, sorry 🙂

    I have my Posts type set as tags, but with no Taxonomy selected.
    I’m using the code below which works to show posts. But if a post has multiple tags it would help if we could edit this to show the first tag only, as I need to narrow it down, is that possible?

    <div class="wpsp-related-posts  grid-container">
        <h4>Read More About This</h4>
         <?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>
    #1245023
    David
    Staff
    Customer Support

    I believe the code the OP posted originally ( click here) worked with the post_tag taxonomy being selected in the WPSP list.

    #1245518
    Brian

    Thanks David, that worked.
    FYI, it shows posts from the last tag created (not last assigned in post editor), just in case you weren’t aware of that.

    #1246176
    David
    Staff
    Customer Support

    You’re welcome – thanks for the heads up

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