How to change WP Query to display related posts

I wrote the code and it works but when I add another Query Loop block (latest posts) to that page it shows the related posts.

That happens because although your filter callback is registered only when blockName is related-posts, the query_loop_block_query_vars hook still applies globally to all Query Loop blocks rendered after that.

How do I change the code to only display related posts in the selected pattern?

If you ever do need to use pre_render_block to register your callback:

  • You can use a variable to ensure your callback is only modifying the query once (e.g. using a static flag).

  • You can unhook your callback after it’s called once using a named function and remove_filter().

You can see working examples here.

The Better Way

As stated in @birgire‘s answer, your callback can accept the second parameter of query_loop_block_query_vars, which includes the block’s context — containing the original attributes from your block/pattern markup.

  • With this approach, there’s no need to use the pre_render_block filter — you can apply your custom logic directly.
add_filter( 'query_loop_block_query_vars', 'filter_query_loop_block_query_vars', 10, 2 );
function filter_query_loop_block_query_vars( $query, \WP_Block $block ) {
    $block_context_query = $block->context['query'];
    if ( isset( $block_context_query['blockName'] ) &&
        'related-posts' === $block_context_query['blockName']
    ) {
        // Add your custom logic here.
    }

    return $query;
}

This method is more robust because it relies on actual block context rather than when the block is rendered, reducing the risk of affecting other Query Loop blocks unintentionally.

The actual code I used (for testing):

add_filter( 'query_loop_block_query_vars', 'filter_query_loop_block_query_vars', 10, 2 );
function filter_query_loop_block_query_vars( $query, \WP_Block $block ) {
    $block_context_query = $block->context['query'];
    if ( isset( $block_context_query['blockName'] ) &&
        'related-posts' === $block_context_query['blockName']
    ) {
        $current_post_id = get_the_ID();
        $post_categories = get_the_category( $current_post_id );

        if ( ! empty( $post_categories ) ) {
            $query['category__in'] = wp_list_pluck( $post_categories, 'term_id' );

            // Use post__not_in sparingly, especially with large sets of post IDs,
            // as it can negatively impact query performance.
            $query['post__not_in'] = array( $current_post_id );
        }
    }

    return $query;
}
  • I used get_the_category() instead of wp_get_post_categories() because the latter doesn’t use caching and will trigger a database query each time it’s called.

  • get_the_category() returns an array of full category objects, so I used wp_list_pluck() to extract just the term IDs needed for the query.

  • This small optimization avoids unnecessary queries and keeps things efficient.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)