forked from scribu/wp-query-multiple-taxonomies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.php
190 lines (137 loc) · 3.86 KB
/
core.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
class QMT_Terms {
private static $filtered_ids;
// Get a list of all the terms attached to all the posts in the current query
public function get( $tax ) {
self::set_filtered_ids();
if ( empty( self::$filtered_ids ) )
return array();
$raw_terms = wp_get_object_terms( self::$filtered_ids, $tax );
// distinct terms
$terms = array();
foreach ( $raw_terms as $term )
$terms[ $term->term_id ] = $term;
return $terms;
}
private function set_filtered_ids() {
global $wp_query;
if ( isset( self::$filtered_ids ) )
return;
$args = array_merge( $wp_query->query, array(
'fields' => 'ids',
'nopaging' => true,
'no_found_rows' => true,
'ignore_sticky_post' => true,
'cache_results' => false,
) );
$query = new WP_Query;
self::$filtered_ids = $query->query( $args );
}
}
class QMT_URL {
public function for_tax( $taxonomy, $value ) {
$query = qmt_get_query();
if ( empty( $value ) )
unset( $query[ $taxonomy ] );
else
$query[ $taxonomy ] = trim( implode( '+', $value ), '+' );
return self::get( $query );
}
public function get( $query = array() ) {
$url = self::get_base();
if ( empty($query) )
return apply_filters( 'qmt_reset_url', $url );
ksort( $query );
foreach ( $query as $taxonomy => $value )
$url = add_query_arg( get_taxonomy( $taxonomy )->query_var, $value, $url );
return apply_filters( 'qmt_url', $url, $query );
}
public function get_base() {
static $base_url;
if ( empty( $base_url ) )
$base_url = apply_filters( 'qmt_base_url', get_bloginfo( 'url' ) );
return trailingslashit( $base_url );
}
}
class QMT_Template {
public function get_title() {
$title = array();
foreach ( qmt_get_query() as $tax => $value ) {
$terms = explode( '+', $value );
$out = array();
foreach ( $terms as $slug ) {
$term_obj = get_term_by( 'slug', $slug, $tax );
if ( $term_obj )
$out[] = $term_obj->name;
}
$tax_obj = get_taxonomy( $tax );
if ( count( $out ) == 1 )
$key = $tax_obj->labels->singular_name;
else
$key = $tax_obj->labels->name;
$title[] .= $key . ': ' . implode( ' + ', $out );
}
return implode( '; ', $title );
}
function wp_title( $title, $sep, $seplocation ) {
$tax_title = QMT_Template::get_title();
if ( empty( $tax_title ) )
return $title;
if ( 'right' == $seplocation )
$title = "$tax_title $sep ";
else
$title = " $sep $tax_title";
return $title;
}
}
add_filter( 'wp_title', array( 'QMT_Template', 'wp_title' ), 10, 3 );
/**
* Wether multiple taxonomies are queried
* @param array $taxonomies A list of taxonomies to check for (AND).
*
* @return bool
*/
function is_multitax( $taxonomies = array() ) {
$queried = array_keys( qmt_get_query() );
$count = count( $taxonomies );
if ( !$count )
return count( $queried ) > 1;
return count( array_intersect( $queried, $taxonomies) ) == $count;
}
/**
* Get the list of selected terms
*
* @param string $taxname a certain taxonomy name
*
* @return array( taxonomy => query )
*/
function qmt_get_query( $taxname = '' ) {
global $wp_query;
$qmt_query = array();
if ( !is_null( $wp_query->tax_query ) ) {
foreach ( $wp_query->tax_query->queries as $tax_query ) {
if ( 'slug' != $tax_query['field'] )
continue;
if ( 'AND' == $tax_query['operator'] )
$qmt_query[ $tax_query['taxonomy'] ] = (array) $tax_query['terms'];
if ( 'IN' == $tax_query['operator'] )
$qmt_query[ $tax_query['taxonomy'] ][] = implode( ',', $tax_query['terms'] );
}
foreach ( $qmt_query as &$value )
$value = implode( '+', $value );
}
if ( $taxname ) {
if ( isset( $qmt_query[ $taxname ] ) )
return $qmt_query[ $taxname ];
return false;
}
return $qmt_query;
}
// Deprecated
function qmt_get_terms( $tax ) {
_deprecated_function( __FUNCTION__, '1.4' );
if ( is_archive() )
return QMT_Terms::get( $tax );
else
return get_terms( $tax );
}