-
Notifications
You must be signed in to change notification settings - Fork 7
/
RCCWP_Query.php
executable file
·83 lines (60 loc) · 1.94 KB
/
RCCWP_Query.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
<?php
define('RC_CWP_QUERY_PERFIX', 'x_');
define('RC_CWP_QUERY_ORDERBY', 'customorderby');
class RCCWP_Query
{
function FilterPrepare(&$qs)
{
global $curr_qs_vars;
$curr_qs_vars = $qs->query_vars;
return $qs;
}
function FilterCustomPostsWhere($where)
{
global $wpdb;
global $curr_qs_vars;
foreach ($curr_qs_vars as $queryVarKey => $queryVarValue){
if (substr($queryVarKey, 0, strlen(RC_CWP_QUERY_PERFIX)) == RC_CWP_QUERY_PERFIX){
$customKey = substr($queryVarKey, strlen(RC_CWP_QUERY_PERFIX));
$customVal = $queryVarValue;
$where = $where . " AND 0 < (SELECT count($wpdb->postmeta.meta_value)
FROM $wpdb->postmeta
WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID and $wpdb->postmeta.meta_key = '$customKey' and $wpdb->postmeta.meta_value = '$customVal') ";
}
}
//Add orderby
if (get_query_var(RC_CWP_QUERY_ORDERBY)){
$newOrderby = get_query_var(RC_CWP_QUERY_ORDERBY);
$newOrderbyFieldName = substr($newOrderby, strlen(RC_CWP_QUERY_PERFIX));
$where = $where . " AND pmeta.meta_key = '$newOrderbyFieldName' ";
}
return $where;
}
function FilterCustomPostsOrderby($orderby)
{
global $wpdb;
if (get_query_var(RC_CWP_QUERY_ORDERBY)){
$newOrderby = get_query_var(RC_CWP_QUERY_ORDERBY);
$newOrderbyFieldName = substr($newOrderby, strlen(RC_CWP_QUERY_PERFIX));
$orderby = "pmeta.meta_value ".get_query_var('order');
}
return $orderby;
}
function FilterCustomPostsFields($fields) {
global $wpdb;
if (get_query_var(RC_CWP_QUERY_ORDERBY)){
$newOrderby = get_query_var(RC_CWP_QUERY_ORDERBY);
$newOrderbyFieldName = substr($newOrderby, strlen(RC_CWP_QUERY_PERFIX));
$fields = $fields. " , pmeta.meta_value ";
}
return $fields;
}
function FilterCustomPostsJoin($join) {
global $wpdb;
if (get_query_var(RC_CWP_QUERY_ORDERBY)){
$join = $join . " INNER JOIN $wpdb->postmeta pmeta ON $wpdb->posts.ID = pmeta.post_id ";
}
return $join;
}
}
?>