Advanced Query Loop use filter for Full and Smart control showing relevant Post with FSE

Challenge: How to Show Relevant Posts List on Single Post Page by Same Taxonomy by Default for Block Query Loop

On the Single Post page, by default, we want to display posts from the same taxonomy (tags or any other custom taxonomies which exist in current post).

In our case, we have a custom post type (CPT), and on the Single Post template’s sidebar, we need to show a list of posts from the same taxonomy, with maximum relevance to the current single post.

We use WordPress (we always use WordPress 😉) with Gutenberg, without PHP templates.
Yes, catch that — we do not use PHP templates, loops, or anything like that.

We rely on default Core Blocks, and for displaying the Query Loop, we use the amazing plugin Advanced Query Loop.

AQL is a fantastic plugin that gives us all the necessary management and filtering options we want and need, but we want to add some smart default logic — for example:

  • Show posts with the same tags as the current post;
  • Filter posts by the same taxonomy terms assigned to the current post.

To start with, we need to:

  • Identify which specific Advanced Query Loop (AQL) block we want to target by its unique blockId.
  • Retrieve the main CPT of the current post.
  • Retrieve additional CPTs that may be set in the block’s settings (Additional CPTs).
  • Merge the main and additional CPTs into a unique array.
  • Determine the taxonomies to filter by (e.g., post_tag).
  • Fetch the taxonomy terms (slugs) of the current post.
  • If the user has manually set taxonomy filters (e.g., selected specific tags in AQL), skip our default logic.
  • Otherwise, add a taxonomy filter to the query arguments to display relevant posts.

Our solution — code:

add_filter( 'aql_query_args', function( $query_args, $block_attrs ) {

    // Specify your target AQL blockId here
    $target_block_id = 'your_block_id_here';

    // Exit if this is not the targeted block
    if ( empty( $block_attrs['blockId'] ) || $block_attrs['blockId'] !== $target_block_id ) {
        return $query_args;
    }

    // Only apply on single post pages
    if ( ! is_singular() ) {
        return $query_args;
    }

    // If user already set manual taxonomy filters, skip our default logic
    if ( ! empty( $query_args['tax_query'] ) ) {
        return $query_args;
    }

    // Get the current post's main CPT
    $current_cpt = get_post_type();

    // Get additional CPTs from block attributes, if any
    $additional_cpts = [];
    if ( ! empty( $block_attrs['additionalPostTypes'] ) && is_array( $block_attrs['additionalPostTypes'] ) ) {
        $additional_cpts = $block_attrs['additionalPostTypes'];
    }

    // Merge main CPT and additional CPTs, ensure unique values
    $all_cpts = array_unique( array_merge( [$current_cpt], $additional_cpts ) );

    // Get current post's tags (slugs)
    $tags = wp_get_post_terms( get_the_ID(), 'post_tag', ['fields' => 'slugs'] );

    if ( ! empty( $tags ) ) {
        // Override post_type with combined CPTs
        $query_args['post_type'] = $all_cpts;

        // Append tax_query for filtering by tags
        if ( isset( $query_args['tax_query'] ) && is_array( $query_args['tax_query'] ) ) {
            $query_args['tax_query'][] = [
                'taxonomy' => 'post_tag',
                'field'    => 'slug',
                'terms'    => $tags,
            ];
        } else {
            $query_args['tax_query'] = [
                [
                    'taxonomy' => 'post_tag',
                    'field'    => 'slug',
                    'terms'    => $tags,
                ],
            ];
        }
    }

    return $query_args;

}, 10, 2 );

Note:

If the user decides to change the filter settings manually (for example, selects a specific tag to filter), our default smart logic will not be applied.


Summary:

What is FSE (Full Site Editing)?
Some developers make custom PHP templates and call it FSE :0)
A block-based theme — that’s real FSE.


P.S.

We provide smart logic by default, and the administrator can always take full advantage of FSE and configure it however they want.