WordPress主题开发:WP_Query常用参数

常用参数用途
调用文章或页面
s 查询和某个关键词相关的所有的文章/页面信息
p 文章或页面id
post__in 多篇id
post__not_in 多篇id以外
post_type 查询的信息类型,默认调用的是文章类型的,post为页面类型
查询某个作者发布的信息
author 按作者id
author_name 按作者别名
author__in 多个作者 'author__in' => array(1, 2)
author__not_in 除了某个作者以外'author__not_in' => array(2)
按分类目录或标签
 cat 按分类目录编号
category_name  按分类目录的别名
category__in 同时查询多个目录id
category__and  既在a目录又在b目录
tag 标签别名
tag_id 按标签编号
按关键字/信息类型/发布日期
 s  通过关键字搜索
  按时间
 year 按年
monthnum
  按日期
分页参数
posts_per_page 每页显示信息数量
paged 分页时显示第几页,需设值获取当前页的分页码:get_query_var('paged')
排序
order 升序降序,默认为'DESC'降序,ASC升序
orderby 按什么排,比如按ID

例子:最新发表文章10篇

<ul>
                    <?php
                        $my_query=new WP_Query(
                            array(
                                'post_type'=>'post',
                                'posts_per_page'=>10,
                                'orderby'=>'date',
                                'order'=>'DESC'
                                 )   
                            );
                        if($my_query->have_posts()):while($my_query->have_posts()):$my_query->the_post();
                      ?>      
                        <li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
                     <?php
                        endwhile;
                        endif;
                    ?>
                    </ul>

注意:多个参数一起用是并列查询的意思,

更多参考:

https://www.wpzhiku.com/all-wp_query-arguments-comments/

https://codex.wordpress.org/Class_Reference/WP_Query#Parameters

原文地址:https://www.cnblogs.com/tinyphp/p/6391972.html