0

From this answer, I'm using the following in a small custom plugin:

function mm_related_posts() {

    if (is_singular()) {
        $related_posts = '';
        $tags = wp_get_post_terms( get_queried_object_id(), 'post_tag', ['fields' => 'ids'] );
        $args = [
            'post__not_in'        => array( get_queried_object_id() ),
            'posts_per_page'      => 5,
            'ignore_sticky_posts' => 1,
            'tax_query' => [
                [
                    'taxonomy' => 'post_tag',
                    'terms'    => $tags
                ]
            ]
        ];
        $my_query = new wp_query( $args );
        if( $my_query->have_posts() ) {
            $related_posts = '<div id="related"><h4>Related Posts</h4>';
                while( $my_query->have_posts() ) {
                    $my_query->the_post();
                    $related_posts .= '<div class="ncc"><h5><a href="' . the_permalink() . '" rel="bookmark" title="' . the_title() . '" rel="nofollow">' . the_title() . '</a></h5>' . the_excerpt() . '</div><!--ncc-->';
                wp_reset_postdata();
                $related_posts .= '</div><!--related-->';
            }
        }
        return $related_posts;
    }
}

// register shortcode
add_shortcode('mm_related_posts', 'mm_related_posts');

However, the shortcode [mm_related_posts] is not outputting any content.

Can you see what is wrong with this code, please?

Help appreciated.

5
  • 2
    Remember what Uncle Wapuu always says: "shortcodes should return their markup as a string, not echo it."
    – bosco
    Commented Aug 29, 2022 at 13:36
  • Thank you @bosco, I've changed my code, but the issue remains.
    – Steve
    Commented Aug 30, 2022 at 3:45
  • @Steve can you check your wp-admin have you added any tags in posts ? If yes can you check $tags has any value and then check $my_query->found_posts has any value.
    – Prits
    Commented Sep 6, 2022 at 8:24
  • Thanks, @Prits, originally I didn't have any tags on posts. I have added display as a tag to 2 posts, and I see $tags = display, and $my_query->found_posts = 1. However, $related_posts .= '<div class="ncc"><h5><a href="' . the_permalink() . '" rel="bookmark" title="' . the_title() . '" rel="nofollow">' . the_title() . '</a></h5>' . the_excerpt() . '</div><!--ncc-->'; is still outputting HTML with no variables output.
    – Steve
    Commented Sep 14, 2022 at 22:54
  • i.e. there is no content between the tags
    – Steve
    Commented Sep 15, 2022 at 7:01

1 Answer 1

0

This code doesnt return anything if the shortcode isnt place on a singular page, also if placed in a singular page and that singular page post has no tags associated will return false, and also if it is on a post with tags associated and the tags are not in more posts also returns empty.

After testing the code, it works if the shortcode is placed in a singular page like for example in a post. but the post has to have tags, and that tags need to be also in others posts. then it will work. I have notice also that the related posts are needing some css to have a better look, but that can depends on the theme. This code works fine when placed in a singular post page, that has tags associated. However the code is not prepare to not have any tag on the post that is in, and show random related.

1
  • I placed the shortcode in a Beaver Builder Themer template used by a specific category of posts. On one of those posts, no content is displayed. If I edit that specific post in Gutenberg and add a Shortcode block with the shortcode [mm_related_posts], nothing is displayed still.
    – Steve
    Commented Sep 6, 2022 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.