View difference between Paste ID: ZwDDRmS4 and rGFeNGm8
SHOW: | | - or go back to the newest paste.
1
function pm_detect_archives($request) {
2
	global $wp, $wpdb;
3
4
5
	$taxonomies = array(
6
		'provincia'
7
	);
8
9
	$taxonomies_2 = array(
10
		'pueblo'
11
	);
12
13
	// Parse the URI
14
	// URL Format: name_of_post_type/taxonomy_term/taxonomy_2_term
15
	preg_match('/([^\/]+)(?:\/([^\/]+))?(?:\/page\/([\d]+))?/', $wp->request, $parts);
16
17
	if(!empty($parts[1])) {
18
		// Get term #1
19
		$term = $wpdb->get_row($wpdb->prepare("
20
			SELECT * FROM {$wpdb->terms}
21
			LEFT JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
22
			WHERE slug = %s",
23
			sanitize_title($parts[1])
24
		));
25
26
		// Get term #2
27-
		if(!empty($parts[3])) {
27+
		if(!empty($parts[2])) {
28
			$term_2 = $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
			
36
37
		// Filter the query if both post type & term were found
38
		if(!empty($term) && in_array($term->taxonomy, $taxonomies)) {
39
			$request = array(
40
				'post_type' => 'restaurantes_y_bares',
41
				'taxonomy' => $term->taxonomy,
42
				'term' => $term->slug,
43
				'do_not_redirect' => 1,
44
			);
45
46
			// Add second term
47
			if(!empty($term_2) && in_array($term_2->taxonomy, $taxonomies_2)) {
48
				$request['taxonomy_2'] = $term_2->taxonomy;
49
				$request['term_2'] = $term_2->slug;	
50
			}
51
52
			// Support pagination
53
			if(!empty($parts[3])) {
54
				$request['paged'] = $parts[3];
55
			}
56
		}
57
	}
58
59
	return $request;
60
}
61
add_filter('request', 'pm_detect_archives', 99);
62
63
function pm_pre_get_posts($query) {
64
	if(!empty($query->query['taxonomy_2']) && $query->is_archive) {
65
		$tax_query = array(
66
			'taxonomy' => $query->query['taxonomy_2'],
67
			'terms' => array($query->query['term_2']),
68
			'field' => 'slug',
69
			'operator'=> 'IN'
70
		);
71
72
		$query->tax_query->queries[] = $tax_query;
73
74
		$query->query_vars['tax_query'] = $query->tax_query->queries;
75
		$query->query_vars['tax_query']['relation'] = 'AND';
76
	}
77
}
78
add_action('pre_get_posts', 'pm_pre_get_posts', 99999);