[Resolved] Related Posts by Tags and Categories [WP Show Posts Pro + GP Hooks]

Home Forums Support [Resolved] Related Posts by Tags and Categories [WP Show Posts Pro + GP Hooks]

Home Forums Support Related Posts by Tags and Categories [WP Show Posts Pro + GP Hooks]

Viewing 15 posts - 1 through 15 (of 42 total)
  • Author
    Posts
  • #1279541
    Christian

    Hello, GP Team!

    I’m currently using GP Hooks and WP Show Posts Pro to display related posts by Tags, however, there are a few Tags that only have 1 post, which makes the code I’m using return the following message:

    “Sorry, no more posts to show”

    I was wondering if it was possible to make it so that if there are no more posts in that Tag, to make it show posts in that Category instead.

    I’m using a Hook code found in another GP thread. Here it is:

    <div class="wpsp-related-posts1  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>

    Thanks in advance πŸ™‚

    #1279988
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    In your list settings, is the taxonomy set to post_tag?

    We may be able to adjust the settings a bit in the PHP.

    Let me know πŸ™‚

    #1280006
    Christian

    Hey, Tombeans!

    Yes, the taxonomy in my list settings is set to post_tag. πŸ™‚

    #1281194
    Tom
    Lead Developer
    Lead Developer

    Ok, let’s try this:

    <div class="wpsp-related-posts1  grid-container">
    	<h2>Related Posts</h2>
    <?php
        $tax = 'post_tag';
    
        if ( is_single() ) {
            $tags =  get_the_tags();
            $tags_list = [];
            $tag_ids = [];
            foreach ($tags as $tag) {
                $tags_list[] = $tag->slug;
                $tag_ids[] = $tag->term_id;
            }
    
            $args = array(
                'numberposts' => 5,
                'tag__in' => $tag_ids,
                'post__not_in' => array( get_the_ID() )
            );
    
            $posts = get_posts( $args );
    
            if ( ! empty( $posts ) ) {
                $tax_term = implode( ', ', $tags_list);
            } else {
                $cats =  get_the_category();
                $text_term = $cats[0]->slug;
                $tax = 'category';
            }
        }
    
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
        ?>
    </div>
    #1281289
    Christian

    Hey, Tom!

    Nope, it’s not working πŸ™‚

    #1281712
    Tom
    Lead Developer
    Lead Developer

    That’s a shame. Can you try the updated code?: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/#post-1281194

    If that doesn’t work we’ll likely need to add some debugging stuff in there to see what’s going on.

    #1281914
    Christian

    Hey, Tom the bomb!

    I tried the updated one, but unfortunately, it didn’t work. I went ahead and deactivated Autoptimize to see if it was maybe some setting there, but nothing changed. πŸ™ πŸ™‚

    #1282802
    Tom
    Lead Developer
    Lead Developer

    Are the tags still working if they exist?

    #1284293
    Christian

    Yes, with the updated code, if the Tag has more than 1 blog post, it still works and shows the related posts.

    Not sure if that’s what you meant by “exist?” πŸ™‚

    #1284580
    Tom
    Lead Developer
    Lead Developer

    Ok, let’s debug:

    <div class="wpsp-related-posts1  grid-container">
    	<h2>Related Posts</h2>
    <?php
        $tax = 'post_tag';
    
        if ( is_single() ) {
            $tags =  get_the_tags();
            $tags_list = [];
            foreach ($tags as $tag) {
                $tags_list[] = $tag->slug;
            }
            
            var_dump($tags_list);
    
            if ( $tags_list ) {
                $tax_term = implode( ', ', $tags_list);
            } else {
                $cats =  get_the_category();
                $text_term = $cats[0]->slug;
                $tax = 'category';
            }
        } else {
            $tax_term = get_tag( get_query_var( 'tag' ) );
    
            if ( ! $tax_term ) {
                $cat = get_category( get_query_var( 'cat' ) );
    
                if ( $cat ) {
                    $text_term = $cats[0]->slug;
                    $tax = 'category';
                }
            }
        }
    
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
        ?>
    </div>

    What outputs on a post that should fall back to the category?

    #1284635
    Christian

    Here’s what outputs on posts that should fall back to the category:

    array(1) { [0]=> string(21) “nutrition-supplements” }
    Sorry, no posts were found.

    #1285047
    Tom
    Lead Developer
    Lead Developer

    Ah, so we need a separate posts query to see if any posts exist.

    Maybe this?: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/#post-1281194

    #1285068
    Christian

    Nope, didn’t work. πŸ™ πŸ™‚

    #1285989
    Tom
    Lead Developer
    Lead Developer
    <div class="wpsp-related-posts1  grid-container">
    	<h2>Related Posts</h2>
    <?php
        $tax = 'post_tag';
    
        if ( is_single() ) {
            $tags =  get_the_tags();
            $tags_list = [];
            $tag_ids = [];
            foreach ($tags as $tag) {
                $tags_list[] = $tag->slug;
                $tag_ids[] = $tag->term_id;
            }
    
            $args = array(
                'numberposts' => 5,
                'tag__in' => $tag_ids,
                'post__not_in' => array( get_the_ID() )
            );
    
            $posts = get_posts( $args );
    
            if ( ! empty( $posts ) ) {
                $tax_term = implode( ', ', $tags_list);
            } else {
                $cats =  get_the_category();
                $text_term = $cats[0]->slug;
                $tax = 'category';
            }
        }
        var_dump('taxonomy=' . $tax . '&tax_term=' . $tax_term);
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
        ?>
    </div>

    What gets output here when no posts exist?

    #1286049
    Christian

    This is what gets output when no posts exist:

    array(1) { [0]=> object(WP_Post)#2457 (24) { [“ID”]=> int(5621) [“post_author”]=> string(1) “3” [“post_date”]=> string(19) “2019-09-22 15:21:40” [“post_date_gmt”]=> string(19) “2019-09-22 19:21:40” [“post_content”]=> string(10243)

    Plus, the whole current blog post. πŸ™‚

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