WPLib_Posts::get_query PHP Метод

get_query() статический публичный Метод

Query the posts. Equivalent to creating a new WP_Query which both instantiates and queries the DB.
См. также: https://github.com/wplib/wplib/commit/8dc27c368e84f7ba6e1448753e1b1f082a60ac6d#commitcomment-11026403
static public get_query ( array $args = [] ) : WPLib_Query
$args array
Результат WPLib_Query
    static function get_query($args = array())
    {
        if ($args instanceof WP_Query) {
            /**
             * @future Fix to return a WPLib_Query, not a WP_Query.
             */
            $query = $args;
        } else {
            if (!isset($args['post_type'])) {
                if ($post_type = static::get_constant('POST_TYPE')) {
                    $args['post_type'] = $post_type;
                }
            }
            if (isset($args['post_type']) && WPLib_Post::POST_TYPE === $args['post_type']) {
                if (!isset($args['order'])) {
                    $args['order'] = 'DESC';
                }
                if (!isset($args['orderby'])) {
                    $args['orderby'] = 'ID';
                }
            }
            $args = wp_parse_args($args, array('post_type' => 'any', 'post_status' => 'publish', 'posts_per_page' => self::max_posts_per_page(), 'index_by' => false, 'orderby' => 'menu_order', 'order' => 'ASC', 'no_found_rows' => true));
            if (!empty($args['post__not_in']) && !is_array($args['post__not_in'])) {
                $args['post__not_in'] = array($args['post__not_in']);
            }
            $query = new WPLib_Query($args);
            if ($args['index_by'] && preg_match('#^(post_(id|name)|id|name)$#', $args['index_by'], $match)) {
                $index_field = 'id' === $match[1] ? 'ID' : 'post_name';
                $posts = array();
                foreach ($query->posts as $post) {
                    $posts[$post->{$index_field}] = $post;
                }
                $query->posts = $posts;
            }
        }
        return $query;
    }

Usage Example

Пример #1
0
 /**
  * Query the posts, return a post list.
  *
  * @param WP_Query|array $args
  * @return WP_Post[]
  */
 static function get_posts($args = array())
 {
     $posts = $args instanceof WP_Query ? $args->posts : WPLib_Posts::get_query($args)->posts;
     return $posts;
 }
All Usage Examples Of WPLib_Posts::get_query