[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 - 16 through 30 (of 42 total)
  • Author
    Posts
  • #1286513
    Tom
    Lead Developer
    Lead Developer

    Adjusted the code above to exclude the current post: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/#post-1285989

    Which post has this ID?: 5621

    Does it have the tag you’re trying to find related posts using?

    #1286581
    Christian

    Yes, post with ID: 5621 has the tag I’m trying to find related posts (but since there are no related posts, it should fallback to posts from the same Category instead).

    The updated code you sent seems to be pulling blog posts now, but from all Categories. It also outputs this before the posts it pulls:

    array(0) { }

    #1287566
    Tom
    Lead Developer
    Lead Developer
    #1287655
    Christian

    It outputs this:

    string(8) “category” NULL

    Still followed by blog posts from all Categories. πŸ™‚

    #1287821
    Tom
    Lead Developer
    Lead Developer

    Does the post have a category? If it’s outputting “NULL”, it means it’s not finding a category for the current post.

    #1287838
    Christian

    Yeap, it has a Category. Just double-checked to make sure.

    #1287853
    Tom
    Lead Developer
    Lead Developer
    #1287865
    Christian

    This one output quite a bit more:

    array(1) { [0]=> object(WP_Term)#2437 (16) { [“term_id”]=> int(596) [“name”]=> string(11) “Weight Loss” [“slug”]=> string(11) “weight-loss” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(596) [“taxonomy”]=> string(8) “category” [“description”]=> string(106) “Research-based weight loss articles to help you create a clear path toward your health and wellness goals.” [“parent”]=> int(0) [“count”]=> int(87) [“filter”]=> string(3) “raw” [“cat_ID”]=> int(596) [“category_count”]=> int(87) [“category_description”]=> string(106) “Research-based weight loss articles to help you create a clear path toward your health and wellness goals.” [“cat_name”]=> string(11) “Weight Loss” [“category_nicename”]=> string(11) “weight-loss” [“category_parent”]=> int(0) } }

    #1288858
    Tom
    Lead Developer
    Lead Developer

    Ok, so that means it should be querying posts that:

    1. Have the taxonomy: category
    2. Have the category weight-loss

    #1289384
    Christian

    Correct! πŸ™‚

    #1290642
    Tom
    Lead Developer
    Lead Developer

    Hmm, I’m not sure what’s up then. The query itself should be fine. I suppose we can debug the query itself.

    First, add the list:

    <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>

    Then add this filter:

    add_filter( 'wp_show_posts_shortcode_args', function( $args ) {
        var_dump($args);
        return $args;
    } );

    Can you share the result of that above the list?

    #1290702
    Christian

    Hey, Tom!

    Here’s what the last code you sent outputs:

    It shows posts from all Categories followed by

    add_filter( ‘wp_show_posts_shortcode_args’, function( $args ) { var_dump($args); return $args; } );

    But don’t worry about it, I’m okay with just leaving it as is! I feel like I’m abusing your support.

    Thank you so much, Tom! πŸ™‚ you’re the bestest.

    #1290933
    Tom
    Lead Developer
    Lead Developer

    Did you add that filter using one of these methods?: https://docs.generatepress.com/article/adding-php/

    No worries! Here if you need me.

    #1290949
    Christian

    Whoops, my bad.

    After adding it to the PHP, it outputs blog posts from all Categories, nothing else.

    #1290951
    Tom
    Lead Developer
    Lead Developer

    No debug info displays at all?

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