forked from welaika/wordless
/
query_helper.php
299 lines (283 loc) · 7.94 KB
/
query_helper.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* This module provides methods for quering the WP database.
*
* @ingroup helperclass
*/
class QueryHelper {
/**
* Get last posts of specified type.
*
* @param string $type
* Post type used to filter result posts.
* @param int $limit (optional)
* Maximum number of post to be retrieved.
* @param string $order (optional)
* The order in which sort the post retrieved. @l{http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters, See WP docs for list of available values}.
* @param string $ord (optional)
* Can be ASC for ascending or DESC for descending.
*
* @return array
* Last posts of the selected type ordered.
*
* @see http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
*
* @ingroup helperfunc
*/
function latest_posts_of_type($type, $limit = -1, $order = 'date', $ord = 'DESC') {
$wp_query = new WP_Query(array(
"posts_per_page" => $limit,
"post_type" => $type,
"orderby" => $order,
"order" => $ord
));
return $wp_query;
}
/**
* Get the last post of the specified type.
*
* @param string $type
* See QueryHelper::latest_posts_of_type().
* @param string $order (optional)
* See QueryHelper::latest_posts_of_type().
*
* @return ??
* Last post of the selected type ordered.
*
* @ingroup helperfunc
*/
function latest_post_of_type($type, $order = 'date') {
return latest_posts_of_type($type, 1, $order);
}
/**
* Get last posts of specified category.
*
* @param string $category
* Name of the category used to filter posts.
* @param int $limit
* Maximum number of post to be retrieved.
* @param int $offset (optional)
* Number of post to skip while retrieveing results.
* @param string $post_type (optional)
* Filter results by post type.
* @param string $taxonomy (optional)
* Taxonomy to query to retrieve posts.
* @param string $order (optional)
* The order in which sort the post retrieved. @l{http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters, See WP docs for list of available values}.
* @param string $ord (optional)
* Can be ASC for ascending or DESC for descending.
*
* @return array
* Last posts of the selected category ordered.
*
* @see http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
*
* @ingroup helperfunc
*/
function latest_posts_of_category($category, $limit, $offset = 0, $post_type = 'post', $taxonomy = 'category', $order = 'date', $ord = 'DESC') {
$wp_query = new WP_Query(array(
'posts_per_page' => $limit,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $category,
),
),
'offset' => $offset,
'post_type' => $post_type,
'orderby' => $order,
'order' => $ord
));
return $wp_query;
}
/**
* Get the last post of the specified category.
*
* @param string $category
* See QueryHelper::latest_posts_of_category().
* @param string $post_type
* See QueryHelper::latest_posts_of_category().
* @param string $taxonomy (optional)
* See QueryHelper::latest_posts_of_category().
*
* @return ??
* Last post of the selected category ordered.
*
* @ingroup helperfunc
*/
function latest_post_of_category($category, $post_type = 'post', $taxonomy = 'category') {
return latest_posts_of_category($category, 1, 0, $post_type, $taxonomy);
}
/**
* Check if post type is the one specified.
*
* @param string $type
* Type of the post to be compared.
*
* @return boolean
* TRUE if the post type of the global $post var is equal to the one passed
* as argument, FALSE otherwise.
*
* @ingroup helperfunc
*/
function is_post_type($type) {
global $post;
return $post->post_type == $type;
}
/**
* Get the first categories, except one.
*
* @param int $limit
* The max number of categories to be retrieved.
* @param string $except
* The name of the category to exclude.
*
* @return string
* A comma separated list of categories or an empty link if
* no category can be found.
*
* @ingroup helperfunc
*/
function get_the_first_categories_except($limit, $except) {
global $post;
$categories = get_the_category();
$found_categories = false;
if (count($categories)) {
$filtered_categories = array();
foreach ($categories as $category) {
if ($category->cat_name != $except and count($filtered_categories) < $limit) {
$filtered_categories[] = link_to($category->cat_name, get_category_link($category->cat_ID));
$found_categories = true;
}
}
}
if ($found_categories) {
return join(", ", $filtered_categories);
} else {
return link_to("Articolo", "#");
}
}
/**
* Returns the page ID from page title.
*
* @param string $title
*
* @return int
* The ID of the page
*
* @ingroup helperfunc
*/
function get_page_id_by_title($title) {
$page = get_page_by_title($title);
return $page->ID;
}
/**
* Returns the category ID from the category name.
*
* @param string $cat_name
* The name of the category
* @param string $taxonomy (optional)
* The taxonomy at which the category belongs to.
*
* @return int
* The ID of the category
*
* @ingroup helperfunc
*/
function get_category_id_by_name($cat_name, $taxonomy = 'category'){
$term = get_term_by('name', $cat_name, $taxonomy);
return $term->term_id;
}
/**
* Returns a link to the specified category.
*
* @param string $cat_name
* The name of the category.
* @param string $taxonomy (optional)
* The taxonomy at which the category
* belongs to
*
* @return int
* A link to the category.
*
* @ingroup helperfunc
*/
function get_category_link_by_name($cat_name, $taxonomy = 'category') {
$id = get_category_id_by_name($cat_name, $taxonomy);
return get_category_link($id);
}
/**
* Returns the content of the current post. Must be called within The Loop.
*
* @return string
* The content of the current post.
*
* @ingroup helperfunc
*/
function get_the_filtered_content() {
ob_start();
the_content();
return ob_get_clean();
}
/**
* Returns the post type (always singular).
*
* @return string
* The post name.
*
* @ingroup helperfunc
*/
function get_post_type_singular_name() {
$obj = get_post_type_object(get_post_type());
return $obj->labels->name;
}
/**
* Returns the page title.
*
* @param string $prefix (optional)
* A string to be prefixed to the current page title.
* @param string $separator (optional)
* A string to separate prefix and current page title.
*
* @return string
* The page title
*
* @ingroup helperfunc
*/
function get_page_title($prefix = "", $separator = "") {
$title = "";
if (is_category()) {
$category = get_category(get_query_var('cat'),false);
$title = get_cat_name($category->cat_ID);
}
if (is_post_type_archive()) {
$title = get_post_type_singular_name();
}
if (is_single() || is_page()) {
$title = get_the_title();
}
if (is_search()) {
$title = sprintf(__("Search: %s", "we"), get_search_query());
}
if (is_date()) {
if (is_month()) {
$date = get_the_date("F Y");
} else if (is_year()) {
$date = get_the_date("Y");
} else {
$date = get_the_date();
}
$title = sprintf(__("Archives: %s", "we"), $date);
}
if (is_front_page() || is_home()) {
$title = get_bloginfo("description", "display");
}
if ($title != "") {
return "$prefix$separator$title";
} else {
return $prefix;
}
}
}
Wordless::register_helper("QueryHelper");