Skip to content

Commit d11affc

Browse files
committed
WP_Query added
1 parent 836585c commit d11affc

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed

autoload.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//Main plugin object to define the plugin
55
if ( ! class_exists( 'PLUGIN_BUILD' ) ) {
6-
6+
77
final class PLUGIN_BUILD {
88

99

@@ -170,6 +170,7 @@ public function functionality() {
170170

171171
require_once ('src/install.php');
172172
require_once ('src/db.php');
173+
require_once ('src/query.php');
173174
require_once ('src/settings.php');
174175
require_once ('src/widget.php');
175176
require_once ('src/metabox.php');
@@ -190,7 +191,7 @@ public function helpers() {
190191

191192
/**
192193
* Available classes:
193-
*
194+
*
194195
* PLUGIN_CORN, PLUGIN_API, PLUGIN_TABLE, PLUGIN_AJAX, PLUGIN_UPLOAD, PLUGIN_SCRIPT
195196
*/
196197
}
@@ -217,4 +218,4 @@ public function __construct() {
217218
$this->settings();
218219
}
219220
}
220-
} ?>
221+
} ?>

src/query.php

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

0 commit comments

Comments
 (0)