Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Query Loop – dynamic posts with a specific tag via SCF (ACF)

  • I have created a content template for specific posts. Each post has several tags. Through secure custom fields I have created a field (slug selected_tag) in which I select one specific tag.

    I need to display a query loop in that content template with the posts that have a tag defined via the selected_tag field.

    Is there any way to do this?

  • Hi there,

    Through secure custom fields I have created a field (slug selected_tag) in which I select one specific tag.

    What is the custom field type? Is it a post-meta or a taxonomy? Can I have more info about it?

  • Hi Ying, custom field type is a taxonomy.

  • Then you should be able to add taxonomy parameter > selected_tag to the query block.

  • Where? On the screen, where the arrow is, you can’t insert it.

  • Not sure what you mean, here’s a screenshot for your reference:
    https://app.screencast.com/1D7jRoDZjCAHU

  • I don’t want to list by category, but mainly by specific tag, which I define via SCF (taxonomy field – tags).
    https://prnt.sc/5KIa0GwxvYO8
    In Query Loop I see only this:
    https://prnt.sc/T5UM_7sIytzD

  • Why not just use the default WP tag?

    I don’t see an advantage of your current setup, or maybe I misunderstood your purpose.

  • Yes, this is a WP post tag. I have several posts, each with several tags. The posts are made using Content Template, so I need the query loop in each post to be generated automatically based on the tag defined via the custom SCF selected_tag field.

  • It’s like defining a primary tag for the current post?

    I don’t think there’s a way out of the box, you will need to write custom PHP to alter the query loop output.

  • This is exactly what I mean.

  • If you are V2 GB block, then try this PHP code, and add selected-tags-posts class to the query block.

    add_filter( 'generateblocks_query_wp_query_args', function( $args, $attributes ) {
        if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'selected-tags-posts' ) !== false ) {
            // Get the current post ID
            $post_id = get_the_ID();
    
            // Retrieve the selected tag(s) from the ACF field
            $selected_tag = get_field( 'selected_tag', $post_id );
    
            if ( $selected_tag && is_array( $selected_tag ) ) {
                // Extract tag IDs
                $tag_ids = wp_list_pluck( $selected_tag, 'term_id' );
    
                // Modify the query arguments to include these tags
                $args = array_merge( 
                    $args, 
                    [
                        'tax_query' => [
                            [
                                'taxonomy' => 'post_tag',
                                'field'    => 'term_id',
                                'terms'    => $tag_ids,
                            ],
                        ],
                    ]
                );
            }
        }
    
        return $args;
    }, 10, 2 );
    
  • I have the latest beta version of GB Block.
    I put the CSS class here:
    https://prnt.sc/E3ybAmFrp_Du
    Is it ok? Because this doesn’t do anything. In the query loop parameters I have the option taxonomy – tags – current post terms.

  • You are using V1 block, are you going to update to v2? it’s now at beta 6 stage.

    If not, then we need to switch to the legacy filter:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'selected-tags-posts' ) !== false ) {
            // Get the current post ID
            $post_id = get_the_ID();
    
            // Retrieve the selected tag(s) from the ACF field
            $selected_tag = get_field( 'selected_tag', $post_id );
    
            if ( $selected_tag && is_array( $selected_tag ) ) {
                // Extract tag IDs
                $tag_ids = wp_list_pluck( $selected_tag, 'term_id' );
    
                // Modify the query arguments to include these tags
                $query_args = array_merge( 
                    $query_args, 
                    [
                        'tax_query' => [
                            [
                                'taxonomy' => 'post_tag',
                                'field'    => 'term_id',
                                'terms'    => $tag_ids,
                            ],
                        ],
                    ]
                );
            }
        }
    
        return $query_args;
    }, 10, 2 );
  • I have the latest beta 6 version of V2 GB Block.

  • Hi there,

    Can you confirm if both GenerateBlocks and GenerateBlocks Pro versions are on V2?

Viewing 16 posts - 1 through 16 (of 38 total)
  • You must be logged in to reply to this topic.