LLMS_Abstract_Posts_Query

Abstract WP_Posts query class.


Description Description

This class is meant to perform custom queries that ultimately get passed into a WP_Query, ideally for a specific post type or list of post types.


Top ↑

Parameters Parameters

$fields

(string) (Required) WP_Post fields to return for each result. Accepts "all", "ids", or "id=>parent". Default: "all".

$status

(string[]|string) (Required) Limit results by WP_Post $post_status. Default: "publish".

$post_types

(string[]) (Required) Limit results to the specified post type(s).


Top ↑

Source Source

File: includes/abstracts/llms-abstract-posts-query.php

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
abstract class LLMS_Abstract_Posts_Query extends LLMS_Abstract_Query {
 
    /**
     * Defines fields that can be sorted on via ORDER BY.
     *
     * @var string[]
     */
    protected $allowed_sort_fields = array(
        'ID',
        'author',
        'title',
        'name',
        'type',
        'date',
        'modified',
        'parent',
        'menu_order',
    );
 
    /**
     * Specify the post types allowed to be queried by this class
     *
     * This array should be a list of one or more post type names.
     *
     * @var string[]
     */
    protected $allowed_post_types = array();
 
    /**
     * The WP_Query instance.
     *
     * @var null
     */
    protected $wp_query = null;
 
    /**
     * Set result counts and pagination properties.
     *
     * @since 6.0.0
     *
     * @return void
     */
    protected function count_results() {
 
        $this->number_results = $this->wp_query->post_count;
        $this->found_results  = $this->found_results();
        $this->max_pages      = (int) $this->wp_query->max_num_pages;
 
    }
 
    /**
     * Retrieve query argument default values.
     *
     * @since 6.0.0
     *
     * @return array
     */
    protected function default_arguments() {
 
        return wp_parse_args(
            array(
                'fields'     => 'all',
                'status'     => 'publish',
                'post_types' => $this->allowed_post_types,
                'sort'       => array(
                    'date' => 'DESC',
                    'ID'   => 'DESC',
                ),
            ),
            parent::default_arguments()
        );
 
    }
 
    /**
     * Retrieve total found results for the query.
     *
     * @since 6.0.0
     *
     * @return int
     */
    protected function found_results() {
        return $this->wp_query->found_posts;
    }
 
    /**
     * Map input arguments to WP_Query arguments.
     *
     * @since 6.0.0
     *
     * @return array
     */
    protected function get_arg_map() {
 
        return array(
            'page'       => 'paged',
            'per_page'   => 'posts_per_page',
            'post_types' => 'post_type',
            'search'     => 's',
            'sort'       => 'orderby',
            'status'     => 'post_status',
        );
 
    }
 
    /**
     * Retrieve the WP_Query object for the query.
     *
     * @since 6.0.0
     *
     * @return WP_Query
     */
    public function get_wp_query() {
        return $this->wp_query;
    }
 
    /**
     * Performs the query.
     *
     * @since 6.0.0
     *
     * @return WP_Post[]|int[] Array of results corresponding to the value specified in the `$fields` query argument.
     */
    protected function perform_query() {
 
        $this->wp_query = new WP_Query( $this->query );
        return $this->wp_query->posts;
 
    }
 
    /**
     * Prepare the query.
     *
     * Should return the query which will be used by `query()`.
     *
     * @since 6.0.0
     *
     * @return mixed
     */
    protected function prepare_query() {
 
        $map = $this->get_arg_map();
 
        $args = array();
        foreach ( $this->query_vars as $var => $val ) {
 
            $var          = array_key_exists( $var, $map ) ? $map[ $var ] : $var;
            $args[ $var ] = $val;
        }
 
        return $args;
 
    }
 
    /**
     * Sets a query variable.
     *
     * Overrides parent method to ensure only allowed post types can be queried.
     *
     * @since 6.0.0
     *
     * @param string $key Variable key.
     * @param mixed  $val Variable value.
     * @return void
     */
    public function set( $key, $val ) {
 
        if ( 'post_types' === $key ) {
            $val = $this->sanitize_post_types( $val );
        }
 
        parent::set( $key, $val );
 
    }
 
    /**
     * Sanitize the `post_types` query argument.
     *
     * Any post types not explicitly included in the `$allowed_post_types` list are
     * removed from the input.
     *
     * @since 6.0.0
     *
     * @param string[] $val Array of post types to query.
     * @return string[] Cleaned array.
     */
    protected function sanitize_post_types( $val ) {
 
        if ( ! is_array( $val ) ) {
            return array();
        }
 
        foreach ( $val as $index => $post_type ) {
            if ( ! in_array( $post_type, $this->allowed_post_types, true ) ) {
                unset( $val[ $index ] );
            }
        }
 
        return array_values( $val );
 
    }
 
}


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.