Archived topic
Related Posts by Post Tags
28 replies · Started by raysn on December 8, 2019
Hi there,
when I was searching for a solution how to implement a related posts area in GeneratePress, I found the following topic: https://generatepress.com/forums/topic/displaying-related-posts/
David suggested to use WP Show Posts (which I also got) together with the code he mentioned in https://gpsites.co/merch/the-single-post/. This is working great for category-related posts.
But I want to set tags as the related posts basis. So is there a way to modify Davids code snippet somehow that only posts with one or more matching tags are getting displayed instead of posts of the same category?
I tried to modify the code but didn't get it work:
<div class="wpsp-related-posts grid-container">
<h2>Related Posts</h2>
<?php
if ( is_single() ) {
$tags = get_the_tags();
$tag = $tags[0];
} else {
$tag = get_tag( get_query_var( 'tag' ) );
}
$tag_slug = $tag->slug;
$list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $tag_slug . '"' );
?>
</div>
Hi there,
can you try editing your WPSP list and set the Taxonomy to post_tag but do not select any Terms. let me know.
Hi David,
thanks for your quick response!
I've changed the settings as you suggested, but unfortunately it still doesn't work as it should, although at least a few posts are displayed now.
The related posts only refer to the first defined tag and all other tags are getting ignored (so less posts are shown). Do you have any idea how I can include all tags a post is tagged with?
Try this:
<div class="wpsp-related-posts 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>
This works fine for me.
Thank you very much!
Awesome - that took a little while to figure out.
Hi David
This works perfectly, but is it possible to take it one step further?
I would like it to show posts by tag, but if there is no tag, to should show posts from the category :)
Hmmm... try this:
<div class="wpsp-related-posts grid-container">
<h2>Related Posts</h2>
<?php
if ( is_single() ) {
// Get the Tags
$tags = get_the_tags();
$tags_list = [];
foreach ($tags as $tag)
$tags_list[] = $tag->slug;
$tags_term = implode( ', ', $tags_list);
// Get the Categories
$cats = get_the_category();
$cat = $cats[0];
$cat_term = $cat->slug;
}
// output Tags or Categories
if ( has_tag() ) {
$tax_term = $tags_term;
} else {
$tax_term = $cat_term;
}
// Display List
$list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $tax_term . '"' );
?>
</div>
That still shows no posts if there's not a tag :(
I guess I could make sure I give everything a tag
Is the post in a Category ?
Yes, all of the posts are in a category.
Dave,
It works, when I was testing I had the show posts set as tag, sorry about that!
So it's all good now, thanks for the help, again :)
Sorry, that was a false dawn.
It now works if a post doesn't have a tag and correctly shows posts from the category.
But doesn't show anything if a post has a tag.
I edited the code above
It now shows nothing if there is a tag, and shows posts from the category if there's no tag.