Advertisement
mbis

Tax combination archive

Feb 7th, 2024
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. function pm_detect_tax_combination( $request ) {
  2.     global $wp, $wpdb;
  3.  
  4.     $taxonomies = array(
  5.         'fuel',
  6.     );
  7.  
  8.     $taxonomies_2 = array(
  9.         'manufacturer'
  10.     );
  11.  
  12.     // Parse the URI
  13.     // URL Format: taxonomy_term/taxonomy_2_term
  14.     preg_match( '/^([^\/]+)(?:\/([^\/]+))?(?:\/page\/([\d]+))?/', $wp->request, $parts );
  15.  
  16.     if ( ! empty( $parts[1] ) && ! empty( $parts[2] ) ) {
  17.         // Get term #1
  18.         $term = $wpdb->get_row( $wpdb->prepare( "
  19.             SELECT * FROM {$wpdb->terms} AS t
  20.            LEFT JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
  21.            WHERE t.slug = %s", sanitize_title( $parts[1] ) ) );
  22.  
  23.         // Get term #2
  24.         $term_2 = $wpdb->get_row( $wpdb->prepare( "
  25.            SELECT * FROM {$wpdb->terms} AS t
  26.            LEFT JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
  27.            WHERE slug = %s", sanitize_title( $parts[2] ) ) );
  28.  
  29.         // Filter the query if both post type & term were found
  30.         if ( ! empty( $term ) && in_array( $term->taxonomy, $taxonomies ) && ! empty( $term_2 ) && in_array( $term_2->taxonomy, $taxonomies_2 ) ) {
  31.             $request = array(
  32.                 $term->taxonomy   => $term->slug,
  33.                 $term_2->taxonomy => $term_2->slug,
  34.                 'do_not_redirect' => 1,
  35.                 'new_canonical'   => 1
  36.             );
  37.  
  38.             // Disable inbuilt canonical redirect
  39.             remove_action( 'template_redirect', 'wp_old_slug_redirect' );
  40.             remove_action( 'template_redirect', 'redirect_canonical' );
  41.  
  42.             // Support pagination
  43.             if ( ! empty( $parts[3] ) ) {
  44.                 $request['paged'] = $parts[3];
  45.             }
  46.         }
  47.     }
  48.  
  49.     return $request;
  50. }
  51. add_filter( 'request', 'pm_detect_tax_combination', 99 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement