Created
February 12, 2026 12:03
-
-
Save everaldomatias/603122b4be909d8240ea702aae300ca5 to your computer and use it in GitHub Desktop.
Filtro para remover tax_query duplicadas da $query do WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Filtro para remover tax_query duplicadas da $query do WordPress | |
| function tax_query_cleanup( $query ) { | |
| if ( is_admin() || ! $query->is_main_query() ) { | |
| return; | |
| } | |
| $tax_query = $query->get( 'tax_query' ); | |
| if ( ! empty( $tax_query ) ) { | |
| $unique_tax_query = []; | |
| $seen = []; | |
| foreach ( $tax_query as $key => $condition ) { | |
| if ( ! is_array( $condition ) || ! isset( $condition['taxonomy'], $condition['terms'] ) ) { | |
| continue; | |
| } | |
| $identifier = $condition['taxonomy'] . '-' . ( is_array( $condition['terms'] ) ? implode( ',', $condition['terms'] ) : $condition['terms'] ); | |
| if ( ! in_array( $identifier, $seen ) ) { | |
| $unique_tax_query[] = $condition; | |
| $seen[] = $identifier; | |
| } | |
| } | |
| // Se limpamos algo, redefinimos a query | |
| if ( count( $unique_tax_query ) !== count( $tax_query ) ) { | |
| $query->set( 'tax_query', $unique_tax_query ); | |
| } | |
| } | |
| } | |
| add_action( 'pre_get_posts', 'tax_query_cleanup', 9999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment