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 need them to have categories and tags. I thought I could just solve the code for the tag and the query parameters would take care of the rest.

  • Have you tried adding the categories using the Add Parameter option? I believe it should work if it doesn’t conflict with the existing code. If it doesn’t work, let us know, and we’ll look into incorporating it into the filter snippet.

  • I tried that and it doesn’t work.

  • Okay, assuming you also have a custom field for the selected categories, you can update the previous code to this:

    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();
            
            // Initialize tax query array
            $tax_query = [];
            
            // Handle tags
            $selected_tags = get_field( 'selected_tag', $post_id );
            if ( $selected_tags && is_array( $selected_tags ) ) {
                $tag_ids = wp_list_pluck( $selected_tags, 'term_id' );
                $tax_query[] = [
                    'taxonomy' => 'post_tag',
                    'field'    => 'term_id',
                    'terms'    => $tag_ids,
                ];
            }
            
            // Handle categories
            $selected_categories = get_field( 'selected_category', $post_id ); // Assuming 'selected_category' is your ACF field name
            if ( $selected_categories && is_array( $selected_categories ) ) {
                $category_ids = wp_list_pluck( $selected_categories, 'term_id' );
                $tax_query[] = [
                    'taxonomy' => 'category',
                    'field'    => 'term_id',
                    'terms'    => $category_ids,
                ];
            }
            
            // If we have any taxonomy queries, add them to the args
            if ( !empty( $tax_query ) ) {
                // If we have both tags and categories, we need to specify the relationship
                if ( count( $tax_query ) > 1 ) {
                    $tax_query['relation'] = 'AND'; // Posts must match both tags AND categories
                }
                
                $args['tax_query'] = $tax_query;
            }
        }
        return $args;
    }, 10, 2 );

    Simply replace the selected category field name with your field name. In this code, I use selected_categories as an example.

  • Awesome. It works. Thank you very much.

  • No problem, glad to hear that!

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