Advertisement
mbis

CPT + 2 custom taxonomies archive

Mar 10th, 2020
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pm_detect_archives($request) {
  2.     global $wp, $wpdb;
  3.  
  4.     $post_types = array(
  5.         'events' => 'events'
  6.     );
  7.  
  8.     $taxonomies = array(
  9.         'solutions'
  10.     );
  11.  
  12.     $taxonomies_2 = array(
  13.         'event_type',
  14.         'category',
  15.         'post_tag',
  16.         'service_line'
  17.     );
  18.  
  19.     // Parse the URI
  20.     // URL Format: name_of_post_type/taxonomy_term/taxonomy_2_term
  21.     preg_match('/([^\/]+)\/([^\/]+)(?:\/([^\/]+))?(?:\/page\/([\d]+))?/', $wp->request, $parts);
  22.  
  23.     if(!empty($parts[1]) && !empty($post_types[$parts[1]])) {
  24.         // Get post type
  25.         $post_type = $post_types[$parts[1]];
  26.  
  27.         // Get term #1
  28.         $term = $wpdb->get_row($wpdb->prepare("
  29.             SELECT * FROM {$wpdb->terms}
  30.             LEFT JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
  31.             WHERE slug = %s",
  32.             sanitize_title($parts[2])
  33.         ));
  34.  
  35.         // Get term #2
  36.         if(!empty($parts[3])) {
  37.             $term_2 = $wpdb->get_row($wpdb->prepare("
  38.                 SELECT * FROM {$wpdb->terms}
  39.                 LEFT JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
  40.                 WHERE slug = %s",
  41.                 sanitize_title($parts[3])
  42.             ));
  43.         }
  44.            
  45.  
  46.         // Filter the query if both post type & term were found
  47.         if(!empty($term) && in_array($term->taxonomy, $taxonomies)) {
  48.             $request = array(
  49.                 'post_type' => $post_type,
  50.                 'taxonomy' => $term->taxonomy,
  51.                 'term' => $term->slug,
  52.                 'do_not_redirect' => 1,
  53.             );
  54.  
  55.             // Add second term
  56.             if(!empty($term_2) && in_array($term_2->taxonomy, $taxonomies_2)) {
  57.                 $request['taxonomy_2'] = $term_2->taxonomy;
  58.                 $request['term_2'] = $term_2->slug;
  59.             }
  60.  
  61.             // Support pagination
  62.             if(!empty($parts[4])) {
  63.                 $request['paged'] = $parts[4];
  64.             }
  65.         }
  66.     }
  67.  
  68.     return $request;
  69. }
  70. add_filter('request', 'pm_detect_archives', 99);
  71.  
  72. function pm_pre_get_posts($query) {
  73.     if(!empty($query->query['taxonomy_2']) && $query->is_archive) {
  74.         $tax_query = array(
  75.             'taxonomy' => $query->query['taxonomy_2'],
  76.             'terms' => array($query->query['term_2']),
  77.             'field' => 'slug',
  78.             'operator'=> 'IN'
  79.         );
  80.  
  81.         $query->tax_query->queries[] = $tax_query;
  82.  
  83.         $query->query_vars['tax_query'] = $query->tax_query->queries;
  84.         $query->query_vars['tax_query']['relation'] = 'AND';
  85.     }
  86. }
  87. add_action('pre_get_posts', 'pm_pre_get_posts', 99999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement