Join the Soledad Facebook Users Group here
for Share, assistance, discussion, and Updates related to Soledad WordPress Theme.

If you can't create a new ticket - you can send us an email via our contact form and we will get back to you.

Okay
  Public Ticket #4510350
Dynamic block filtering by tag in current category
Open

Comments

  • jeroenkl1 started the conversation

    Hi,

    I’m using the Soledad theme with Penci Blocks to build dynamic country pages on my travel website. Here’s what I want to achieve:

    • Each country has its own category (e.g. South Korea, Japan, Morocco).

    • On each category page, I want to display posts that belong to that current category AND have a specific tag, for example cycling).

    • I would like to create one reusable block or template that automatically adapts to the current category — so I don’t need to make a separate block for every country.

    At the moment, I can make the block dynamic by choosing

    Source → “Archive Builder / Current Query”
    but then I can’t filter by tag anymore.

    When I switch to a custom query, I can include tags, but then the block is no longer dynamic (it doesn’t inherit the current category).

    Question:
    Is there a way to combine both — so that a Penci Block (or Archive Template section) dynamically shows posts from the current category filtered by a fixed tag (for example “cycling”)?

    If not directly, is there a recommended workaround (for example via shortcode parameter, query ID, or Elementor dynamic tag)?

    Thanks a lot for your help!
    Best regards,
    Jeroen

  •  2,996
    PenciDesign replied

    Hi,

    You should paste this code into the function.php of the child theme or via the CodeSnippet plugin here: https://wordpress.org/plugins/code-snippets/

    /**
     * Combine category posts with posts tagged with tag ID 4
     * Works on category archive pages only.
     */
    add_action( 'pre_get_posts', function( $query ) {
        // Only modify the main frontend category archive query
        if ( 
            ! is_admin() 
            && $query->is_main_query() 
            && $query->is_category()
        ) {
            // Get current category ID
            $category = get_queried_object();
            $category_id = $category->term_id ?? 0;
    
            // Combine category + tag=4
            $tax_query = [
                'relation' => 'OR',
                [
                    'taxonomy' => 'category',
                    'field'    => 'term_id',
                    'terms'    => $category_id,
                ],
                [
                    'taxonomy' => 'post_tag',
                    'field'    => 'term_id',
                    'terms'    => [4],
                ],
            ];
    
            // Set the new tax query
            $query->set( 'tax_query', $tax_query );
    
            // Ensure posts are ordered by date (newest first)
            $query->set( 'orderby', 'date' );
            $query->set( 'order', 'DESC' );
    
            // Optional: define posts per page if needed
            // $query->set( 'posts_per_page', 10 );
        }
    });
    
    /**
     * Remove duplicate posts if they appear in both category & tag.
     */
    add_filter( 'posts_distinct', function( $distinct, $query ) {
        if ( 
            $query->is_main_query() 
            && $query->is_category() 
        ) {
            return 'DISTINCT';
        }
        return $distinct;
    }, 10, 2 );
    

    Replace [4] with the tag ID that you want to show.

    Regards,
    PenciDesign.