shortcode – get_queried_object_id / short code returning blank

Question

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

function mm_related_posts() {

    if (is_singular()) {
        $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() ) {
            echo '<div id="related"><h4>Related Posts</h4>';
                while( $my_query->have_posts() ) {
                    $my_query->the_post(); ?>
                    <div class="ncc">

                        <h5><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>" rel="nofollow"><?php the_title(); ?></a></h5>
                        <?php the_excerpt(); ?>

                    </div><!--ncc-->
                <?php }
                wp_reset_postdata();
            echo '</div><!--related-->';
        }
    }
}

// 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.

0
Steve 4 weeks 2022-08-28T20:53:00-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse