Last active
July 17, 2020 14:27
-
-
Save petrusnog/5853b1174b34059bbe74125d1eb5f041 to your computer and use it in GitHub Desktop.
Filtrar termos de produtos filtrados por tax e meta query. (Ex: Exibir as categorias de todos os produtos fora de estoque)
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 | |
| function get_filtered_terms ( $post_type, $taxonomy, $tax_query = 0, $meta_query = 0 ) { | |
| //Filtra os posts de acordo com a filtragem passada | |
| $args = [ | |
| "post_type" => $post_type, | |
| "posts_per_page" => -1, | |
| ]; | |
| if ( $tax_query ) { | |
| $args["tax_query"] = [ | |
| "tax_query" => array($tax_query), | |
| ]; | |
| } | |
| if ( $meta_query ) { | |
| $args["meta_query"] = [ | |
| "meta_query" => array($meta_query), | |
| ]; | |
| } | |
| $posts = new WP_Query($args); | |
| //Array que abrigará os termos da filtragem | |
| $terms = []; | |
| $i = 0; | |
| //Alimentação do array $terms | |
| if ( $posts->have_posts() ) { | |
| while ( $posts->have_posts() ) { $posts->the_post(); | |
| $continue = 0; | |
| $product_terms = get_the_terms( get_the_ID(), $taxonomy ); | |
| if ( $product_terms ) { | |
| foreach ( $product_terms as $pt ) { | |
| foreach ( $terms as $array ) { | |
| if ( $array["name"] == $pt->name ) { | |
| $continue = 1; | |
| break; | |
| } | |
| } | |
| if ( $continue ){ | |
| continue; | |
| } | |
| array_push( $terms, [ | |
| "name" => $pt->name, | |
| "term" => $pt, | |
| ] ); | |
| } | |
| } | |
| } | |
| } | |
| return $terms; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment