1
|
|
|
<?php |
2
|
|
|
namespace NirjharLo\WP_Plugin_Framework\Src; |
3
|
|
|
|
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* WP Query class for querying WP database |
8
|
|
|
* For more reference of arguments visit: https://gist.github.com/nirjharlo/5c6f8ac4cc5271f88376788e599c287b |
9
|
|
|
* This class depends on WP pagenavi plugin: https://wordpress.org/plugins/wp-pagenavi/ |
10
|
|
|
* |
11
|
|
|
* @author Nirjhar Lo |
12
|
|
|
* @package wp-plugin-framework |
13
|
|
|
*/ |
14
|
|
|
if ( ! class_exists( 'Query' ) ) { |
15
|
|
|
|
16
|
|
|
class Query { |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Int |
21
|
|
|
*/ |
22
|
|
|
public $display_count; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The post and user querying |
27
|
|
|
* |
28
|
|
|
* @return Void |
29
|
|
|
*/ |
30
|
|
|
public function __construct() { |
31
|
|
|
|
32
|
|
|
global $post, $user; |
33
|
|
|
|
34
|
|
|
$this->display_count = get_option('posts_per_page', 10); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$paged = get_query_var('paged') ? get_query_var('paged') : (get_query_var('page') ? get_query_var('page') : 1); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$post_args = $this->post_args($paged); |
|
|
|
|
39
|
|
|
$the_query = new WP_Query( $post_args ); |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
$user_args = $this->user_args($paged); |
43
|
|
|
$the_query = new WP_User_Query( $user_args ); |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
// The Loop |
47
|
|
|
if ( $the_query->have_posts() ) { |
48
|
|
|
while ( $the_query->have_posts() ) { |
49
|
|
|
$the_query->the_post(); |
50
|
|
|
// Do Stuff |
51
|
|
|
} // end while |
52
|
|
|
} // endif |
53
|
|
|
|
54
|
|
|
if ( function_exists('wp_pagenavi' ) ) { |
55
|
|
|
wp_pagenavi( array( 'query' => $the_query, 'echo' => true ) );//For user query add param 'type' => 'users' |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Reset Post Data |
59
|
|
|
wp_reset_postdata(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* User query arguments |
65
|
|
|
* |
66
|
|
|
* @return Void |
67
|
|
|
*/ |
68
|
|
|
public function user_args( $paged ) { |
69
|
|
|
|
70
|
|
|
$offset = ( $paged - 1 ) * $this->display_count; |
71
|
|
|
|
72
|
|
|
$args = array ( |
73
|
|
|
'role' => '', // user role |
74
|
|
|
'order' => '', //ASC or DESC |
75
|
|
|
'fields' => '', // |
76
|
|
|
'orderby' => '', |
77
|
|
|
'role__in' => '', |
78
|
|
|
'role__not_in' => '', |
79
|
|
|
'include' => '', |
80
|
|
|
'exclude' => '', |
81
|
|
|
'blog_id' => '', |
82
|
|
|
'taxonomy' => '', |
83
|
|
|
'terms' => '', |
84
|
|
|
'number' => $this->display_count, |
85
|
|
|
'count_total' => true, |
86
|
|
|
'paged' => $paged, |
87
|
|
|
'offset' => $offset, |
88
|
|
|
'search' => '*' . esc_attr( $_GET['s'] ) . '*', |
|
|
|
|
89
|
|
|
'meta_query' => array( // It supports nested meta query |
90
|
|
|
'relation' => 'AND', //or 'OR' |
91
|
|
|
array( |
92
|
|
|
'key' => 'meta_key', |
93
|
|
|
'value' => 'some_value', |
94
|
|
|
'compare' => 'LIKE' //like others |
95
|
|
|
), |
96
|
|
|
array( |
97
|
|
|
'key' => 'meta_key', |
98
|
|
|
'value' => 'some_value', |
99
|
|
|
'compare' => 'LIKE' //like others |
100
|
|
|
) |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return $args; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Post query arguments |
110
|
|
|
* |
111
|
|
|
* @return Void |
112
|
|
|
*/ |
113
|
|
|
public function post_args( $paged ) { |
114
|
|
|
|
115
|
|
|
$offset = ( $paged - 1 ) * $this->display_count; |
116
|
|
|
|
117
|
|
|
$args = array ( |
118
|
|
|
'post_type' => '', // array of type slugs |
119
|
|
|
'order' => 'ASC', |
120
|
|
|
'fields' => '', // |
121
|
|
|
'orderby' => '', |
122
|
|
|
'include' => '', |
123
|
|
|
'exclude' => '', |
124
|
|
|
'blog_id' => '', |
125
|
|
|
'taxonomy' => '', |
126
|
|
|
'terms' => '', |
127
|
|
|
'number' => $this->display_count, |
128
|
|
|
'count_total' => true, |
129
|
|
|
'paged' => $paged, |
130
|
|
|
'offset' => $offset, |
131
|
|
|
'search' => '*' . esc_attr( $_GET['s'] ) . '*', |
|
|
|
|
132
|
|
|
'meta_query' => array( // It supports nested meta query |
133
|
|
|
'relation' => 'AND', //or 'OR' |
134
|
|
|
array( |
135
|
|
|
'key' => 'meta_key', |
136
|
|
|
'value' => 'some_value', |
137
|
|
|
'compare' => 'LIKE' //like others |
138
|
|
|
), |
139
|
|
|
array( |
140
|
|
|
'key' => 'meta_key', |
141
|
|
|
'value' => 'some_value', |
142
|
|
|
'compare' => 'LIKE' //like others |
143
|
|
|
) |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
return $args; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} ?> |
|
|
|
|
151
|
|
|
|