1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Post; |
4
|
|
|
|
5
|
|
|
use WP_Query; |
6
|
|
|
use Silk\Query\Builder as BaseBuilder; |
7
|
|
|
|
8
|
|
|
class QueryBuilder extends BaseBuilder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* WP_Query instance |
12
|
|
|
* |
13
|
|
|
* @var WP_Query |
14
|
|
|
*/ |
15
|
|
|
protected $query; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Builder constructor. |
19
|
|
|
* |
20
|
|
|
* @param WP_Query $query |
21
|
|
|
*/ |
22
|
|
|
public function __construct(WP_Query $query = null) |
23
|
|
|
{ |
24
|
|
|
if (! $query) { |
25
|
|
|
$query = new WP_Query(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->query = $query; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new instance. |
33
|
|
|
* |
34
|
|
|
* @param WP_Query $query |
35
|
|
|
* |
36
|
|
|
* @return static |
37
|
|
|
*/ |
38
|
|
|
public static function make(WP_Query $query = null) |
39
|
|
|
{ |
40
|
|
|
return new static($query); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Limit the number of returned results |
45
|
|
|
* |
46
|
|
|
* @param integer $limit The maximum number of results to return |
47
|
|
|
* use -1 for no limit |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function limit($limit) |
52
|
|
|
{ |
53
|
|
|
return $this->set('posts_per_page', (int) $limit); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return an unlimited number of results. |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function all() |
62
|
|
|
{ |
63
|
|
|
return $this->limit(-1); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the order for the query |
68
|
|
|
* |
69
|
|
|
* @param string $order |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function order($order) |
74
|
|
|
{ |
75
|
|
|
return $this->set('order', strtoupper($order)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Query by post status |
80
|
|
|
* |
81
|
|
|
* @param string|array $status the post status or stati to match |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function whereStatus($status) |
86
|
|
|
{ |
87
|
|
|
return $this->set('post_status', $status); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Query by slug |
92
|
|
|
* |
93
|
|
|
* @param string $slug the post slug to query by |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function whereSlug($slug) |
98
|
|
|
{ |
99
|
|
|
return $this->set('name', $slug); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set a query variable on the query |
104
|
|
|
* |
105
|
|
|
* @param string $var Query variable key |
106
|
|
|
* @param mixed $value Query value for key |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function set($var, $value) |
111
|
|
|
{ |
112
|
|
|
$this->query->set($var, $value); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Execute the query and return the raw results. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function query() |
123
|
|
|
{ |
124
|
|
|
if ($this->model) { |
125
|
|
|
$this->set('post_type', $this->model->post_type) |
|
|
|
|
126
|
|
|
->set('fields', ''); // as WP_Post objects |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->query->get_posts(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.