Created
March 18, 2020 13:44
-
-
Save chriswagoner/ed1fa30646ee7aff8bf081be6881357f to your computer and use it in GitHub Desktop.
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
| // ================================================================================================ | |
| // FLIPPING THE GET SHORTCODE TO GET CATS IN A RESTRICTED FASHION ^CW | |
| // ================================================================================================ | |
| /** | |
| * Eg : [getcats type="slug"] | |
| * Shortcode for getting a list of categories from a current product category & restricted by product brand | |
| * | |
| * Shortcode attributes : | |
| * type (optional): type of resultset values returned (slug or id) Default: slug | |
| * | |
| * returns comma-separated string of brand slugs or brand ids | |
| */ | |
| function get_category_from_brands( $atts ) | |
| { | |
| $current_category = get_queried_object(); | |
| $res_cats = array(); | |
| $type = ( $atts['type'] == 'id' ) ? 'term_id' : 'name'; | |
| if ( $current_category instanceof WP_Term ) { | |
| // get all deepest level of childs slug | |
| $childrens = get_deepest_level_childs( $current_category ); | |
| foreach ( $childrens as $child ) { | |
| // Get all products under specified product category slug (taxonomy) | |
| $post_args = array( | |
| 'posts_per_page' => -1, | |
| 'post_type' => 'product', | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'product_cat', | |
| 'field' => 'slug', | |
| 'terms' => $child, | |
| ) | |
| ) | |
| ); | |
| $posts_array = get_posts( $post_args ); | |
| if ( empty( $posts_array ) ) { | |
| continue; | |
| } | |
| // Get all post ids | |
| $post_ids = array_column( $posts_array, 'ID' ); | |
| // Get all tabs for above post ids | |
| $post_args_2 = array( | |
| 'posts_per_page' => -1, | |
| 'post_type' => 'product', | |
| 'post__in' => $post_ids, | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'pa_brandname', | |
| 'field' => 'slug', | |
| 'terms' => $_GET["brand"], | |
| ) | |
| ) | |
| ); | |
| $ids_filtered = array_column( $posts_array, 'ID' ); | |
| // If no brands were found for all above posts under given category | |
| if ( !empty( $ids_filtered ) ) { | |
| if ( $type === "term_id" ) { | |
| $res_cats[] = get_term_by( "slug", $child, "product_cat" )->term_id; | |
| } else { | |
| $res_cats[] = $child; | |
| } | |
| } | |
| } | |
| $terms_direct = array_column( get_terms( | |
| 'product_cat', | |
| array( | |
| 'parent' => $current_category->term_id, | |
| ) | |
| ), "slug" ); | |
| $res_cats = array_diff( $res_cats, array_diff( $res_cats, $terms_direct ) ); | |
| } | |
| $result = implode( ',', $res_cats ); | |
| return $result; | |
| } | |
| add_shortcode('getcats', 'get_category_from_brands'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment