- This topic has 41 replies, 2 voices, and was last updated 3 years, 4 months ago by Elvin.
-
AuthorPosts
-
May 15, 2020 at 4:40 pm #1286513TomLead DeveloperLead 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?
May 15, 2020 at 6:01 pm #1286581ChristianYes, 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) { }
May 16, 2020 at 10:50 am #1287566TomLead DeveloperLead DeveloperCool, that’s progress.
More testing: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/#post-1285989
What does that output?
May 16, 2020 at 11:48 am #1287655ChristianIt outputs this:
string(8) “category” NULL
Still followed by blog posts from all Categories. π
May 16, 2020 at 3:00 pm #1287821TomLead DeveloperLead DeveloperDoes the post have a category? If it’s outputting “NULL”, it means it’s not finding a category for the current post.
May 16, 2020 at 3:11 pm #1287838ChristianYeap, it has a Category. Just double-checked to make sure.
May 16, 2020 at 3:22 pm #1287853TomLead DeveloperLead DeveloperK, let’s keep going. We will get this… lol
What does this tell you? https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/#post-1285989
May 16, 2020 at 3:44 pm #1287865ChristianThis 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) } }
May 17, 2020 at 9:42 am #1288858TomLead DeveloperLead DeveloperOk, so that means it should be querying posts that:
1. Have the taxonomy:
category
2. Have the categoryweight-loss
May 17, 2020 at 8:22 pm #1289384ChristianCorrect! π
May 18, 2020 at 11:43 am #1290642TomLead DeveloperLead DeveloperHmm, 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?
May 18, 2020 at 12:30 pm #1290702ChristianHey, 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.
May 18, 2020 at 4:22 pm #1290933TomLead DeveloperLead DeveloperDid you add that filter using one of these methods?: https://docs.generatepress.com/article/adding-php/
No worries! Here if you need me.
May 18, 2020 at 4:42 pm #1290949ChristianWhoops, my bad.
After adding it to the PHP, it outputs blog posts from all Categories, nothing else.
May 18, 2020 at 4:46 pm #1290951TomLead DeveloperLead DeveloperNo debug info displays at all?
-
AuthorPosts
- You must be logged in to reply to this topic.