海天无影Blog

WordPress主查询函数query_posts()使用方法和参数

/ 2692阅 / 2评 / 0

query_posts()查询函数决定了哪些文章出现在WordPress 主循环(loop)中,query_posts函数仅用于修改主页循环(loop),而不是在页面上生成的次级循环。如果要在主循环外另外生成循环,应该新建独立的WP_Query对象,用WP_Query对象生成循环。在主循环外的循环上使用query_posts会导致主循环运行偏差。

query_posts()查询函数可以使用大量参数,调用方式可以使用字符串,也可以使用数组。

query_posts()调用方式:

字符串形式:

query_posts('page_id=7'); //获取页面ID为7的页面

数组形式:

query_posts(array(
'cat' => 22,
'year' => $current_year,
'monthnum' => $current_month,
'order' => 'ASC',
));

query_posts()调用参数:

分类参数

只显示特定分类下的文章。

根据ID显示单个分类

只显示来自某一个分类目录ID(以及该分类目录下的子分类目录)的文章:

query_posts('cat=4');

根据分类名称显示单个分类

只显示来自某一个分类名称下的文章:

query_posts('category_name=Staff Home');

根据ID显示多个分类

显示来自若干指定分类目录ID下的文章:

query_posts('cat=2,6,17,38');

排除某一分类中的文章

显示除某一分类文章外的所有文章,被排除的分类ID以减号(’-')作为前缀。

query_posts('cat=-3');

以上代码删除ID为3的分类中的文章。

处理多个分类

显示隶属于多个分类的文章。下面的代码可展示同时属于分类2和分类6的文章:

query_posts(array('category__and' => array(2,6)));

如果希望显示分类2或分类6中的文章,可以使用上面介绍的cat,也可以使用category_in函数 (注意这里不会显示分类下子分类中的文章) :

query_posts(array('category__in' => array(2,6)));

可以用下面这种方式排除多个分类中的文章:

query_posts(array('category__not_in' => array(2,6)));

标签参数

显示特定标签下的文章。

获取某一标签中的文章

query_posts('tag=cooking');

获取若干标签中任一标签中的文章

query_posts('tag=bread+baking+recipe');

多个标签

显示同时属于ID为37和47的标签下的文章:

query_posts(array('tag__and' => array(37,47));

若要显示ID为为37或47的标签下的文章,可以使用tag参数,也可以用tag_in:

query_posts(array('tag__in' => array(37,47));

显示的文章既不属于标签37,也不属于标签47:

query_posts(array('tag__not_in' => array(37,47));

tag_slug_in与tag_slug_and工作方式几乎一致,不同之处在于相匹配的别名不同。

作者参数

你也可以根据作者来选择文章。

注意:author_name运行在user_nicename字段上,同时author运行在author id字段上。

显示ID为1的作者所发表的所有页面,以标题顺序排列页面,页面列表上方无置顶文章:

query_posts('caller_get_posts=1&author=1&post_type=page&post_status=publish&orderby=title&order=ASC');

文章&页面参数

检索单篇文章或页面。

置顶文章参数

置顶文章功能引入于WordPress 2.7。在查询中,被设为“置顶”的文章会显示在其它文章之前,除非该文章已经被caller_get_posts=1 参数排除。

返回第一篇置顶文章

$sticky=get_option('sticky_posts') ;
query_posts('p=' . $sticky[0]);

$args = array(
'posts_per_page' => 1,
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1
);
query_posts($args);

注意:第二种方法只能返回最新发表的置顶文章;若当前无置顶文章,返回最新发表文章。

返回第一篇置顶文章;若无,则不返回任何内容

$sticky = get_option('sticky_posts');
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'caller_get_posts' => 1
);
query_posts($args);
if($sticky[0]) {
// insert here your stuff...
}

从查询中排除所有置顶文章

query_posts(array("post__not_in" =>get_option("sticky_posts")));

返回某一分类下所有文章,但不在文章列表上方显示置顶文章。所有设为“置顶”的文章以正常顺序(如日期顺序)显示

query_posts('caller_get_posts=1&posts_per_page=3&cat=6');

返回某一分类下所有文章,完全不显示置顶文章,保留分页

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
'cat'=>3,
'caller_get_posts'=>1,
'post__not_in' => $sticky,
'paged'=>$paged,
);
query_posts($args);
?>

时间参数

检索特定时间段内发表的文章。

返回最近发表的文章

$today = getdate();
query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );

返回12月20日发表的文章

query_posts(monthnum=12&day=20' );

返回2009年3月1日到3月15日之间发表的文章

<?php
//based on Austin Matzko's code from wp-hackers email list
function filter_where($where = '') {
//posts for March 1 to March 15, 2009
$where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

返回最近30天内发表的文章

<?php
//based on Austin Matzko's code from wp-hackers email list
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

返回过去30天到过去60天内发表的文章

<?php
//based on Austin Matzko's code from wp-hackers email list
function filter_where($where = '') {
//posts 30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

分页参数

offset(偏移)参数

通过offset参数,你可以移除或忽略正常情况下被查询集中的一篇或多篇初始文章。

以下显示最近一篇文章之后的5篇文章:

query_posts('posts_per_page=5&offset=1');

排序参数

顺序参数

决定以升序或降序排列排序参数

自定义字段参数

根据自定义关键字或值检索文章(或页面)。

返回关键字为 ‘color’ 且值为’blue’的文章:

query_posts('meta_key=color&metavalue=blue');

返回自定义字段关键字为’color’的文章,无论自定义字段值为何:

query_posts('meta_key=color');

返回自定义字段值为’color’的文章,无论关键字为何:

query_posts('metavalue=color');

返回自定义字段值为’green’的页面,无论自定义字段关键字为何:

query_posts('post_type=page&metavalue=green');

返回自定义关键字为’color’、自定义字段值不为’blue’的文章和页面:

query_posts('post_type=any&meta_key=color&meta_compare=!=&metavalue=blue');

返回自定义字段关键字为’miles’、自定义字段值小于等于22的文章。注意,字段值99会被看做大于字段值100,因为数据是以字符串形式而不是数字形式存储的。

query_posts('meta_key=miles&meta_compare=<=&metavalue=22');

《 “WordPress主查询函数query_posts()使用方法和参数” 》 有 2 条评论

  1. 莫良说道:

    已经无力折腾wp的代码,当年的博客很多都灰飞烟灭。。。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注