- This topic has 41 replies, 2 voices, and was last updated 3 years, 4 months ago by Elvin.
-
AuthorPosts
-
May 18, 2020 at 4:56 pm #1290961Christian
lol, sorry. Not sure what’s up with me today.
Here it is:
array(7) { [“order”]=> string(4) “desc” [“orderby”]=> string(4) “rand” [“post_type”]=> string(4) “post” [“posts_per_page”]=> int(6) [“ignore_sticky_posts”]=> bool(true) [“post_status”]=> array(1) { [0]=> string(7) “publish” } [“post__not_in”]=> array(1) { [0]=> int(5621) } }
May 19, 2020 at 9:49 am #1292350TomLead DeveloperLead DeveloperSo our taxonomy stuff isn’t making it into the query at all. That’s very strange – not sure what could be conflicting there.
<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 does this output?
May 19, 2020 at 11:02 am #1292460ChristianWould you like me to keep the filter?
With the filter:
string(27) “taxonomy=category&tax_term=”
array(7) { [“order”]=> string(4) “desc” [“orderby”]=> string(4) “rand” [“post_type”]=> string(4) “post” [“posts_per_page”]=> int(6) [“ignore_sticky_posts”]=> bool(true) [“post_status”]=> array(1) { [0]=> string(7) “publish” } [“post__not_in”]=> array(1) { [0]=> int(5621) } }Without the filter:
string(27) “taxonomy=category&tax_term=”May 19, 2020 at 4:45 pm #1292894TomLead DeveloperLead DeveloperLol, I spelled the variable name wrong, unbelievable.
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(); $tax_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>
May 19, 2020 at 5:21 pm #1292918ChristianSmh, Tom…
Kidding. Okay, I think you got it. This one’s working perfectly! π
Thank you so much, Tom!
May 20, 2020 at 6:48 am #1293719raysnHmm I am getting the following php error message when using the code mentioned above:
Warning: Invalid argument supplied for foreach() in /www/htdocs/xxxxxx/xxxxxx/wp-content/plugins/gp-premium/elements/class-hooks.php(180) : eval()’d code on line 24
Seems like this error only occurs, when there is no tag set.
By the way: I’m using the related posts element within the before_right_sidebar_content hook.
The Plugin query monitor outputs the following information:
May 20, 2020 at 9:57 am #1294168TomLead DeveloperLead DeveloperHere it is cleaned up:
<div class="wpsp-related-posts1 grid-container"> <h2>Related Posts</h2> <?php $tax = 'post_tag'; $tax_term = ''; if ( is_single() ) { $tags = get_the_tags(); $tags_list = []; $tag_ids = []; foreach ( (array) $tags as $tag) { if ( isset( $tag->slug ) ) { $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 ) && ! empty( $tag_ids ) ) { $tax_term = implode( ', ', $tags_list); } else { $cats = get_the_category(); if ( ! empty( $cats ) ) { $tax_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>
Let me know if that fixes it or not π
May 20, 2020 at 8:32 pm #1294691ChristianWhoops, I keep forgetting to mark these as resolved, heh. π
May 27, 2020 at 2:00 am #1303156raysnHey Tom,
although there is no critical error anymore, I still get 2 minor PHP issues (“Trying to get property of non-object”). I made a screenshot of the messages (have a look on the full-size image).
Greetings
RaysnMay 27, 2020 at 9:58 am #1303823TomLead DeveloperLead DeveloperLikely means there are no categories/tags in your case. Updated the code to check for empties: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/page/3/#post-1294168
November 25, 2020 at 1:25 am #1545669AndreiHey guys, I’m having a similar issue with no posts available to be shown. I follow this guide https://gpsites.co/merch/the-single-post/ – How I made the Related Posts. It works great just some of my categories have 1 article and I get that no posts available notice.
What I would like to achieve in the end would be to display related posts by category and if none available to have a fallback on most recent published articles.
Is this possible?
Cheers.
November 25, 2020 at 11:52 am #1546982ElvinStaffCustomer SupportHi Andrei,
As this is already resolved for the topic starter, can you open up a new topic?
So you could use the Private information text field incase needed. Thank you. π
-
AuthorPosts
- You must be logged in to reply to this topic.