更好的WordPress主题

WordPress 获取文章函数 query_posts() 的使用方法和参数介绍

摘要:获取文章列表和内容的函数

query_posts() 是一个非常强大的文章功能函数。

query_posts() 的使用方法:

$args = array(
    // 参数设置
);
query_posts($args);
while (have_posts()) : the_post(); 

endwhile; 
wp_reset_query();

query_posts() 的参数:

@type int          $attachment_id           Attachment post ID. Used for 'attachment' post_type.
@type int|string   $author                  Author ID, or comma-separated list of IDs.
@type string       $author_name             User 'user_nicename'.
@type array        $author__in              An array of author IDs to query from.
@type array        $author__not_in          An array of author IDs not to query from.
@type bool         $cache_results           Whether to cache post information. Default true.
@type int|string   $cat                     Category ID or comma-separated list of IDs (this or any children).
@type array        $category__and           An array of category IDs (AND in).
@type array        $category__in            An array of category IDs (OR in, no children).
@type array        $category__not_in        An array of category IDs (NOT in).
@type string       $category_name           Use category slug (not name, this or any children).
@type array|int    $comment_count           Filter results by comment count. Provide an integer to match
                                            comment count exactly. Provide an array with integer 'value'
                                            and 'compare' operator ('=', '!=', '>', '>=', '<', '<=' ) to
                                            compare against comment_count in a specific way.
@type string       $comment_status          Comment status.
@type int          $comments_per_page       The number of comments to return per page.
                                            Default 'comments_per_page' option.
@type array        $date_query              An associative array of WP_Date_Query arguments.
                                            See WP_Date_Query::__construct().
@type int          $day                     Day of the month. Default empty. Accepts numbers 1-31.
@type bool         $exact                   Whether to search by exact keyword. Default false.
@type string|array $fields                  Which fields to return. Single field or all fields (string),
                                            or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
                                            Default all fields. Accepts 'ids', 'id=>parent'.
@type int          $hour                    Hour of the day. Default empty. Accepts numbers 0-23.
@type int|bool     $ignore_sticky_posts     Whether to ignore sticky posts or not. Setting this to false
                                            excludes stickies from 'post__in'. Accepts 1|true, 0|false.
                                            Default 0|false.
@type int          $m                       Combination YearMonth. Accepts any four-digit year and month
                                            numbers 1-12. Default empty.
@type string       $meta_compare            Comparison operator to test the 'meta_value'.
@type string       $meta_compare_key        Comparison operator to test the 'meta_key'.
@type string       $meta_key                Custom field key.
@type array        $meta_query              An associative array of WP_Meta_Query arguments. See WP_Meta_Query.
@type string       $meta_value              Custom field value.
@type int          $meta_value_num          Custom field value number.
@type string       $meta_type_key           Cast for 'meta_key'. See WP_Meta_Query::construct().
@type int          $menu_order              The menu order of the posts.
@type int          $monthnum                The two-digit month. Default empty. Accepts numbers 1-12.
@type string       $name                    Post slug.
@type bool         $nopaging                Show all posts (true) or paginate (false). Default false.
@type bool         $no_found_rows           Whether to skip counting the total rows found. Enabling can improve
                                            performance. Default false.
@type int          $offset                  The number of posts to offset before retrieval.
@type string       $order                   Designates ascending or descending order of posts. Default 'DESC'.
                                            Accepts 'ASC', 'DESC'.
@type string|array $orderby                 Sort retrieved posts by parameter. One or more options may be
                                            passed. To use 'meta_value', or 'meta_value_num',
                                            'meta_key=keyname' must be also be defined. To sort by a
                                            specific `$meta_query` clause, use that clause's array key.
                                            Accepts 'none', 'name', 'author', 'date', 'title',
                                            'modified', 'menu_order', 'parent', 'ID', 'rand',
                                            'relevance', 'RAND(x)' (where 'x' is an integer seed value),
                                            'comment_count', 'meta_value', 'meta_value_num', 'post__in',
                                            'post_name__in', 'post_parent__in', and the array keys
                                            of `$meta_query`. Default is 'date', except when a search
                                            is being performed, when the default is 'relevance'.
@type int          $p                       Post ID.
@type int          $page                    Show the number of posts that would show up on page X of a
                                            static front page.
@type int          $paged                   The number of the current page.
@type int          $page_id                 Page ID.
@type string       $pagename                Page slug.
@type string       $perm                    Show posts if user has the appropriate capability.
@type string       $ping_status             Ping status.
@type array        $post__in                An array of post IDs to retrieve, sticky posts will be included
@type string       $post_mime_type          The mime type of the post. Used for 'attachment' post_type.
@type array        $post__not_in            An array of post IDs not to retrieve. Note: a string of comma-
                                            separated IDs will NOT work.
@type int          $post_parent             Page ID to retrieve child pages for. Use 0 to only retrieve
                                            top-level pages.
@type array        $post_parent__in         An array containing parent page IDs to query child pages from.
@type array        $post_parent__not_in     An array containing parent page IDs not to query child pages from.
@type string|array $post_type               A post type slug (string) or array of post type slugs.
                                            Default 'any' if using 'tax_query'.
@type string|array $post_status             A post status (string) or array of post statuses.
@type int          $posts_per_page          The number of posts to query for. Use -1 to request all posts.
@type int          $posts_per_archive_page  The number of posts to query for by archive page. Overrides
                                            'posts_per_page' when is_archive(), or is_search() are true.
@type array        $post_name__in           An array of post slugs that results must match.
@type string       $s                       Search keyword(s). Prepending a term with a hyphen will
                                            exclude posts matching that term. Eg, 'pillow -sofa' will
                                            return posts containing 'pillow' but not 'sofa'. The
                                            character used for exclusion can be modified using the
                                            the 'wp_query_search_exclusion_prefix' filter.
@type int          $second                  Second of the minute. Default empty. Accepts numbers 0-60.
@type bool         $sentence                Whether to search by phrase. Default false.
@type bool         $suppress_filters        Whether to suppress filters. Default false.
@type string       $tag                     Tag slug. Comma-separated (either), Plus-separated (all).
@type array        $tag__and                An array of tag ids (AND in).
@type array        $tag__in                 An array of tag ids (OR in).
@type array        $tag__not_in             An array of tag ids (NOT in).
@type int          $tag_id                  Tag id or comma-separated list of IDs.
@type array        $tag_slug__and           An array of tag slugs (AND in).
@type array        $tag_slug__in            An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
                                            true. Note: a string of comma-separated IDs will NOT work.
@type array        $tax_query               An associative array of WP_Tax_Query arguments.
                                            See WP_Tax_Query->queries.
@type string       $title                   Post title.
@type bool         $update_post_meta_cache  Whether to update the post meta cache. Default true.
@type bool         $update_post_term_cache  Whether to update the post term cache. Default true.
@type bool         $lazy_load_term_meta     Whether to lazy-load term meta. Setting to false will
                                            disable cache priming for term meta, so that each
                                            get_term_meta() call will hit the database.
                                            Defaults to the value of `$update_post_term_cache`.
@type int          $w                       The week number of the year. Default empty. Accepts numbers 0-53.
@type int          $year                    The four-digit year. Default empty. Accepts any four-digit year.

本文属原创,转载请注明原文:https://themebetter.com/query_posts.html

感觉很棒,欢迎打赏~

10 打赏
分享到:

评论 (0)

登录后评论
注:有关主题的售前售后请 提交工单

服务器低至¥89元/年 限时抢

安全、稳定、高效,阿里云服务器专享1折起,限时抢各种代金券

立即抢购

建站!你有充足的理由选择我们

世界上超过30%的网站是由WordPress搭建,而我们是国内最靠谱的WordPress开发商
QQ咨询 在线咨询 公众号