Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. function wp_se_273314( $query ) {
  2. if ( ! $query->is_tax() || ! isset( $query->query['product_cat'] ) ) {
  3. return;
  4. }
  5.  
  6. $levels = explode( '/', $query->query['product_cat'] );
  7. $current_index = count( $levels ) - 1;
  8.  
  9. $current_term = null;
  10. foreach ( $query->tax_query->queries as &$tax_query ) {
  11. if ( $tax_query['taxonomy'] == 'product_cat' ) {
  12. if ( $current_index > 0 ) {
  13. $parent = get_term_by( 'slug', $levels[ $current_index - 1 ], 'product_cat' );
  14. $term_children = get_term_children( $parent->term_id, 'product_cat' );
  15.  
  16. foreach ( $term_children as $child ) {
  17. $term = get_term( $child );
  18. if ( $term->slug == $levels[ $current_index ] ) {
  19. $current_term = $term;
  20. }
  21. }
  22.  
  23. if ( is_null( $current_term ) ) {
  24. die( 'An error occurred when retrieving terms' );
  25. }
  26. $index = array_search( $levels[ $current_index ], $tax_query['terms'] );
  27. if ( $index !== false ) {
  28. $tax_query['field'] = 'id';
  29. $tax_query['operator'] = '=';
  30. $tax_query['terms'][ $index ] = $current_term->term_id;
  31.  
  32. }
  33. }
  34. }
  35. }
  36. $query->tax_query->queried_terms['product_cat']['terms'][0] = $current_term->term_id;
  37. $query->tax_query->queried_terms['product_cat']['field'] = 'id';
  38.  
  39. $query->queried_object->term_id = $current_term->term_id;
  40. $query->queried_object_id = $current_term->term_id;
  41. $query->queried_object->term_taxonomy_id = $current_term->term_taxonomy_id;
  42. $query->queried_object->parent = $current_term->parent;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement