Home › Forums › Pro Support › Related Post
- This topic has 40 replies, 3 voices, and was last updated 2 years, 2 months ago by
Tom.
-
AuthorPosts
-
October 27, 2020 at 1:25 am #21710
Samuel
ParticipantHi,
You proposal is returning a “Sorry, no posts were found.” message.
BUT, I tried to replace
$cat = $cats[0];
with$cat = $cats[1];
and it’s working. I didn’t know that[O]
was Category (first level) and[1]
was Sub-Category (second level).Problem now is that if posts are just in first level, it’s returning a “Sorry, no posts were found.” message.
Would it be possible to say “Look in
$cats[1]
but if there is nothing in there then look in$cats[0]
” ?Or “look in
$cats[1 and O]
in this order” (priority to 1)October 27, 2020 at 2:17 pm #21761Tom
KeymasterHmm, what about this?:
if ( is_single() ) { $cat = get_the_category(); } else { $cat = get_category( get_query_var( 'cat' ) ); }
If that doesn’t work, we may be able to loop through them all and build our own array of categories.
October 28, 2020 at 12:53 am #21783Samuel
ParticipantHi Tom,
This is not working, returning a “no found” message.
October 28, 2020 at 1:52 pm #21830Tom
KeymasterOk, let’s try this:
if ( is_single() ) { $cat = array(); $cats = get_the_category(); foreach ( $cats as $cat ) { $cat[] = $cat->slug; } } else { $cat = get_category( get_query_var( 'cat' ) ); }
October 28, 2020 at 2:41 pm #21837Samuel
ParticipantHi,
This is provoquing a critical error.
We agree that the entire function would be like this ?
<?php if ( is_single() ) { $cat = array(); $cats = get_the_category(); foreach ( $cats as $cat ) { $cat[] = $cat->slug; } } else { $cat = get_category( get_query_var( 'cat' ) ); } $cat_slug = $cat->slug; $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' ); ?>
October 29, 2020 at 2:02 pm #21904Tom
KeymasterTry this:
<?php if ( is_single() ) { $cat_values = array(); $cats = get_the_category(); foreach ( $cats as $cat ) { $cat_values[] = $cat->slug; } } else { $cat_values = get_category( get_query_var( 'cat' ) ); } $cat_slug = implode( ', ', $cat_values ); $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' ); wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' ); ?>
October 30, 2020 at 4:16 am #21954Samuel
ParticipantHi Tom,
It’s not working, it’s breaking my layout and displaying a “critical error” message where WPSP is supposed to display.
October 30, 2020 at 11:41 am #22004Tom
KeymasterWhat’s the error, exactly?
Using WPSP like this does take some tweaking, as it wasn’t built for it by default.
October 30, 2020 at 12:33 pm #22008Samuel
ParticipantHere it is with Wp-debug=True
Fatal error: Uncaught Error: Cannot use object of type WP_Term as array in /home/movoco/public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(196) : eval()'d code:10 Stack trace: #0 /home/movoco/public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(196): eval() #1 /home/movoco/public_html/wp-includes/class-wp-hook.php(287): GeneratePress_Hook->execute_hook('') #2 /home/movoco/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array) #3 /home/movoco/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #4 /home/movoco/public_html/wp-content/themes/generatepress/content-single.php(99): do_action('generate_after_...') #5 /home/movoco/public_html/wp-includes/template.php(732): require('/home/movoco/pu...') #6 /home/movoco/public_html/wp-includes/template.php(676): load_template('/home/movoco/pu...', false, Array) #7 /home/movoco/public_html/wp-includes/general-template.php(204): locate_template(Array, true, false, Array) #8 /home/movoco/public_html/wp-content/them in /home/movoco/public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(196) : eval()'d code on line 10
November 2, 2020 at 2:42 pm #22113Tom
KeymasterCan you try the updated code?: https://wpshowposts.com/support/topic/related-post/page/2/#post-21904
November 3, 2020 at 6:37 am #22131Samuel
ParticipantHi Tom,
Unfortunately this snipet is displaying posts from category but not from sub-category.
November 3, 2020 at 2:57 pm #22171Tom
KeymasterCan you share the debug data with this change?
November 3, 2020 at 3:06 pm #22175Samuel
ParticipantHi Tom,
Here you go :
array(7) { ["order"]=> string(3) "ASC" ["orderby"]=> string(6) "parent" ["post_type"]=> string(4) "post" ["posts_per_page"]=> int(4) ["post_status"]=> array(1) { [0]=> string(7) "publish" } ["tax_query"]=> array(1) { [0]=> array(4) { ["taxonomy"]=> string(8) "category" ["field"]=> string(4) "slug" ["terms"]=> array(2) { [0]=> string(6) ""andes" [1]=> string(10) "santander"" } ["operator"]=> string(2) "IN" } } ["post__not_in"]=> array(1) { [0]=> int(22765) } }
November 4, 2020 at 2:57 pm #22213Tom
KeymasterSo that means the list is now looking for posts in the “andes” and “santander” categories.
Things get pretty complex once you start checking for parent/child categories, which is why related post plugins are much better suited for this kind of thing.
Is “santander” a child category within the “andes” category?
November 4, 2020 at 3:11 pm #22221Samuel
ParticipantHi Tom,
Yes, “santander” a child category within the “andes” category
-
AuthorPosts
- You must be logged in to reply to this topic.