Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_detect_archives($request) {
- global $wp, $wpdb;
- $taxonomies = array(
- 'provincia'
- );
- $taxonomies_2 = array(
- 'pueblo'
- );
- // Parse the URI
- // URL Format: name_of_post_type/taxonomy_term/taxonomy_2_term
- preg_match('/([^\/]+)(?:\/([^\/]+))?(?:\/page\/([\d]+))?/', $wp->request, $parts);
- if(!empty($parts[1])) {
- // Get term #1
- $term = $wpdb->get_row($wpdb->prepare("
- SELECT * FROM {$wpdb->terms}
- LEFT JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
- WHERE slug = %s",
- sanitize_title($parts[1])
- ));
- // Get term #2
- if(!empty($parts[2])) {
- $term_2 = $wpdb->get_row($wpdb->prepare("
- SELECT * FROM {$wpdb->terms}
- LEFT JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
- WHERE slug = %s",
- sanitize_title($parts[2])
- ));
- }
- // Filter the query if both post type & term were found
- if(!empty($term) && in_array($term->taxonomy, $taxonomies)) {
- $request = array(
- 'post_type' => 'restaurantes_y_bares',
- 'taxonomy' => $term->taxonomy,
- 'term' => $term->slug,
- 'do_not_redirect' => 1,
- );
- // Add second term
- if(!empty($term_2) && in_array($term_2->taxonomy, $taxonomies_2)) {
- $request['taxonomy_2'] = $term_2->taxonomy;
- $request['term_2'] = $term_2->slug;
- }
- // Support pagination
- if(!empty($parts[3])) {
- $request['paged'] = $parts[3];
- }
- }
- }
- return $request;
- }
- add_filter('request', 'pm_detect_archives', 99);
- function pm_pre_get_posts($query) {
- if(!empty($query->query['taxonomy_2']) && $query->is_archive) {
- $tax_query = array(
- 'taxonomy' => $query->query['taxonomy_2'],
- 'terms' => array($query->query['term_2']),
- 'field' => 'slug',
- 'operator'=> 'IN'
- );
- $query->tax_query->queries[] = $tax_query;
- $query->query_vars['tax_query'] = $query->tax_query->queries;
- $query->query_vars['tax_query']['relation'] = 'AND';
- }
- }
- add_action('pre_get_posts', 'pm_pre_get_posts', 99999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement