View difference between Paste ID: rGFeNGm8 and BNgXYAmr
SHOW: | | - or go back to the newest paste.
1
function pm_detect_archives($request) {
2
	global $wp, $wpdb;
3
4-
	$post_types = array(
4+
5-
		'events' => 'events'
5+
6
		'provincia'
7
	);
8
9-
		'solutions'
9+
10
		'pueblo'
11
	);
12
13-
		'event_type',
13+
14-
		'category',
14+
15-
		'post_tag',
15+
	preg_match('/([^\/]+)(?:\/([^\/]+))?(?:\/page\/([\d]+))?/', $wp->request, $parts);
16-
		'service_line'
16+
17
	if(!empty($parts[1])) {
18
		// Get term #1
19
		$term = $wpdb->get_row($wpdb->prepare("
20
			SELECT * FROM {$wpdb->terms}
21-
	preg_match('/([^\/]+)\/([^\/]+)(?:\/([^\/]+))?(?:\/page\/([\d]+))?/', $wp->request, $parts);
21+
22
			WHERE slug = %s",
23-
	if(!empty($parts[1]) && !empty($post_types[$parts[1]])) {
23+
			sanitize_title($parts[1])
24-
		// Get post type
24+
25-
		$post_type = $post_types[$parts[1]];
25+
26
		// Get term #2
27
		if(!empty($parts[3])) {
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])
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-
				sanitize_title($parts[3])
41+
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-
				'post_type' => $post_type,
49+
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-
			if(!empty($parts[4])) {
62+
63-
				$request['paged'] = $parts[4];
63+
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);