-
honzapecenka
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.
-
Alvind
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.
-
honzapecenka
I tried that and it doesn’t work.
-
Alvind
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. -
honzapecenka
Awesome. It works. Thank you very much.
-
Alvind
No problem, glad to hear that!
- You must be logged in to reply to this topic.