文章目录
按 Author 来调用文章
指定作者ID来调用文章
$query = new WP_Query( ‘author=123′ );
按照作者昵称来调用文章
$query = new WP_Query( ‘author_name=mingzi’ );
按ID调用多个作者的文章
$query = new WP_Query( ‘author=2,6,17,38′ );
过滤掉某个作者的文章
$query = new WP_Query( ‘author=-12′ );
按 Category 来调用文章
按分类ID调用某个分类的文章
$query = new WP_Query( ‘cat=4′ );
按分类别名指定调用某个分类的文章
$query = new WP_Query( ‘category_name=fenlei’ );
按分类ID调用多个分类文章
$query = new WP_Query( ‘cat=2,6,17,38′ );
按分类别名调用多个分类文章
$query = new WP_Query( ‘category_name=fenlei,news’ );
过滤掉某些分类ID
$query = new WP_Query( ‘cat=-12,-34,-56′ );
调用同时属于分类ID为2和6的两个分类下的文章(交集)
$query = new WP_Query( array( ‘category__and’ => array( 2, 6 ) ) );
调用属于分类ID为2或者6下的文章
$query = new WP_Query( array( ‘category__in’ => array( 2, 6 ) ) );
调用不属于分类ID为2或者6下的文章
$query = new WP_Query( array( ‘category__not_in’ => array( 2, 6 ) ) );
按 Tag 调用文章
调用含有标签 cooking 的文章
$query = new WP_Query( ‘tag=cooking’ );
按标签ID调用文章
$query = new WP_Query( ‘tag_id=13′ );
调用标签 bread,baking 的文章
$query = new WP_Query( ‘tag=bread,baking’ );
调用同时含有标签 bread,baking,recipe 的文章(交集)
$query = new WP_Query( ‘tag=bread+baking+recipe’ );
调用标签ID为37和47的文章(交集)
$query = new WP_Query( array( ‘tag__and’ => array( 37, 47 ) ) );
调用标签ID为37或47的文章
$query = new WP_Query( array( ‘tag__in’ => array( 37, 47 ) ) );
过滤掉标签ID为37或47的文章
$query = new WP_Query( array( ‘tag__not_in’ => array( 37, 47 ) ) );
按 Post 和 Page 调用文章
指定文章的ID调用文章
$query = new WP_Query( ‘p=7′ );
指定页面的ID调用页面
$query = new WP_Query( ‘page_id=7′ );
指定文章别名调用文章
$query = new WP_Query( ‘name=about-my-life’ );
指定页面别名调用页面
$query = new WP_Query( ‘pagename=contact’ );
别名特殊调用,指定层次关系下(parent_slug/child_slug)
$query = new WP_Query( ‘pagename=contact_us/canada’ );
按ID和文章属性调用多个页面
$query = new WP_Query( array( ‘post_type’ => ‘page’, ‘post__in’ => array( 2, 5, 12, 14, 20 ) ) );
按ID过滤掉多个页面
$query = new WP_Query( array( ‘post__not_in’ => array( 2, 5, 12, 14, 20 ) ) );
属性调用
调用全部页面
$query = new WP_Query( ‘post_type=page’ );
调用自定义文章(WordPress 3.4 以上适用)
$query = new WP_Query( array( ‘post_type’ => array( ‘post’, ‘page’, ‘movie’, ‘book’ ) ) );
状态调用
可用的文章状态有:
- ‘publish‘ – 已发布
- ‘pending‘ – 审核中
- ‘draft‘ – 草稿
- ‘auto-draft‘ – 自动草稿
- ‘future‘ – 定时发布
- ‘private‘ – 私有
- ‘inherit‘ – 修订版(自动存档)
- ‘trash‘ – 回收站
- ‘any‘ – 任何
调用草稿
$query = new WP_Query( ‘post_status=draft’ );
多重调用
$query = new WP_Query( array( ‘post_status’ => array( ‘pending’, ‘draft’, ‘future’ ) ) );
调用附件
$query = new WP_Query( array( ‘post_status’ => ‘any’, ‘post_type’ => ‘attachment’ ) );
按分页调用
一页显示10篇文章
$query = new WP_Query( ‘posts_per_page=10′ );
所有文章在一页显示
$query = new WP_Query( ‘posts_per_page=-1′ );
显示第6页的文章
$query = new WP_Query( ‘paged=6′ );
Display posts from current page
$query = new WP_Query( ‘paged=’ . get_query_var( ‘paged’ ) );
Display posts from the current page and set the ‘paged’ parameter to 1 when the query variable is not set (first page).
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$query = new WP_Query( ‘paged=’ . $paged );
Display posts from current page on a static front page
$paged = (get_query_var(‘page’)) ? get_query_var(‘page’) : 1;
$query = new WP_Query( ‘paged=’ . $paged );
文章过滤
过滤掉前3篇文章
$query = new WP_Query( ‘offset=3′ ) );
每5篇文章分页一次,并且过滤掉第一篇文章
$query = new WP_Query( array( ‘posts_per_page’ => 5, ‘offset’ => 1 ) );
文章排序
按文章名倒序排列
$query = new WP_Query( array ( ‘orderby’ => ‘title’, ‘order’ => ‘DESC’ ) );
随机显示5篇文章
$query = new WP_Query( array ( ‘orderby’ => ‘rand’, ‘posts_per_page’ => ‘5’ ) );
按评论数量排列
$query = new WP_Query( array( ‘orderby’ => ‘comment_count’ ) );
置顶文章
显示第一篇置顶文章
$sticky = get_option( ‘sticky_posts’ );
$query = new WP_Query( ‘p=’ . $sticky[0] );
显示第一篇置顶文章,如果没有置顶文章则返回到最新的文章
$args = array(
‘posts_per_page’ => 1,
‘post__in’ => get_option( ‘sticky_posts’ ),
‘ignore_sticky_posts’ => 1
);
$query = new WP_Query( $args );
显示第一篇置顶文章,如果没有置顶文章则不显示任何内容
$sticky = get_option( ‘sticky_posts’ );
$args = array(
‘posts_per_page’ => 1,
‘post__in’ => $sticky,
‘ignore_sticky_posts’ => 1
);
$query = new WP_Query( $args );
if ( $sticky[0] ) {
// insert here your stuff…
}
不显示置顶文章
$query = new WP_Query( array( ‘post__not_in’ => get_option( ‘sticky_posts’ ) ) );
显示分类ID为6的分类下所有文章,并且每3篇分一页,不让置顶文章显示在最顶部
$query = new WP_Query( ‘ignore_sticky_posts=1&posts_per_page=3&cat=6′ );
排除置顶文章,返回到分类最新文章,按系统分页规则分页
$paged = get_query_var( ‘paged’ ) ? get_query_var( ‘paged’ ) : 1;
$sticky = get_option( ‘sticky_posts’ );
$args = array(
‘cat’ => 3,
‘ignore_sticky_posts’ => 1,
‘post__not_in’ => $sticky,
‘paged’ => $paged
);
