• Hi there people of WP,

    I am building a block template for a custom post type. I’d like to showcase related posts (i.a.w. posts in the same taxonomy) using the Query Block. However, the current post is not excluded from the query.

    I have searched the forums and seen multiple topics from 1+ years ago, which concluded that it’s not possible. Like other WP enthusiasts on here, I also think it’s a feature people have come to expect when using a module that pulls in a list of posts within a post template. I was able to locate the GitHub issue, however what’s not clear to me is whether I and other developers can expect something like this to roll out?

    In the meantime, I’d like to know whether anyone has discovered a good workaround/alternative. Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Mayuri

    (@mayuripatel)

    Hello ,

    Below code gets all posts of category related to current post EXCEPT current post , so you can call function current_cats_related_post() to display output.

    function current_cats_related_post() {
    
        $post_id = get_the_ID();
        $cat_ids = array();
        $categories = get_the_category( $post_id );
    
        if(!empty($categories) && !is_wp_error($categories)):
            foreach ($categories as $category):
                array_push($cat_ids, $category->term_id);
            endforeach;
        endif;
    
        $current_post_type = get_post_type($post_id);
    
        $query_args = array( 
            'category__in'   => $cat_ids,
            'post_type'      => $current_post_type,
            'post__not_in'    => array($post_id),
            'posts_per_page'  => '3',
         );
    
        $related_cats_post = new WP_Query( $query_args );
    
        if($related_cats_post->have_posts()):
             while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
                <ul>
                    <li>
                        <a href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                        <?php the_content(); ?>
                    </li>
                </ul>
            <?php endwhile;
    
            // Restore original Post Data
            wp_reset_postdata();
         endif;
    
    }

    @mayuripatel Hi Please help as my website is not working getting error

    This site can’t be reached

    learnion.blackbuck.com took too long to respond.

    Try:

    ERR_CONNECTION_TIMED_OUT

    Mayuri

    (@mayuripatel)

    Hello @pawsak

    there are many different reasons this error can occur .

    here is link which explains why this error comes and how to resolve it : https://www.hostinger.com/tutorials/fix-err_connection_timed_out#What_Does_the_ERR_CONNECTION_TIMED_OUT_Error_Mean

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Excluding current post from Query Loop block for showing related posts’ is closed to new replies.