have_posts 修改
Many WordPress plugins retrieve posts from the database by customizing the sorting order, retrieving posts based on a specific meta key or taxonomy. Have you ever wondered how these plugins retrieve customized lists of posts without writing any SQL queries? In this tutorial we’ll learn how to do exactly that.
许多WordPress插件通过自定义排序顺序从数据库检索帖子,并根据特定的元键或分类检索帖子。 您是否曾经想过这些插件如何在不编写任何SQL查询的情况下检索自定义的帖子列表? 在本教程中,我们将学习如何做到这一点。
In this article we’ll explore the get_posts()
function with some examples of how to use it in your own projects. We’ll also cover some typical use cases for this function and how it’s different from the WP_Query
object and get_pages
function.
在本文中,我们将通过一些示例说明如何在自己的项目中使用get_posts()
函数。 我们还将介绍此函数的一些典型用例,以及与WP_Query
对象和get_pages
函数的区别。
get_posts()
函数? (What is the get_posts()
Function?)The get_posts
function has been available in WordPress core since WordPress 1.2.0. This function is basically used to retrieve posts from the database by applying custom filters and sorting the final result based on a set of parameters.
自WordPress 1.2.0起, get_posts
函数已在WordPress核心中可用。 该功能基本上用于通过应用自定义过滤器并基于一组参数对最终结果进行排序来从数据库检索帖子。
The get_posts()
function returns an array of WP_Post
objects. Each WP_Post
object represents an individual post.
get_posts()
函数返回WP_Post
对象的数组。 每个WP_Post
对象代表一个单独的帖子。
Internally get_posts
uses the WP_Query
object to construct and execute the SQL queries based on the passed set of parameters.
在内部, get_posts
使用WP_Query
对象根据传递的参数集构造和执行SQL查询。
Note: Posts means post, page and custom post type.
注意:帖子是指帖子,页面和自定义帖子类型。
WP_Query
对象? (Why Not Use the WP_Query
Object Directly?)Plugins use get_posts
function instead of WP_Query
object because using the WP_Query
object directly alters the main loop (i.e., the global $wp_query
variable) which would cause site issues.
插件使用get_posts
函数而不是WP_Query
对象,因为使用WP_Query
对象会直接更改主循环(即全局$wp_query
变量),这将导致站点问题。
get_posts()
和get_pages()
函数之间有什么区别? (What Is the Difference Between get_posts()
and get_pages()
Functions?)Both of them are used to retrieve posts from the WordPress database, however, here are some of the differences between them:
两者都用于从WordPress数据库中检索帖子,但是,这是它们之间的一些区别:
The get_pages()
function currently does not retrieve posts based on meta_key
and meta_value
parameters.
get_pages()
函数当前不基于meta_key
和meta_value
参数检索帖子。
The get_pages()
function doesn’t use the WP_Query
object. Instead, it constructs and executes SQL queries directly.
get_pages()
函数不使用WP_Query
对象。 而是直接构造和执行SQL查询。
get_posts()
函数参数 (get_posts()
Function Parameters)The get_posts
function takes only one argument as an array. The array contains the different parameters to apply custom filters and sort the result.
get_posts
函数仅将一个参数作为数组。 该数组包含不同的参数,以应用自定义过滤器并对结果进行排序。
Here’s a code example which shows how to call this function and the various parameters available:
这是一个代码示例,显示了如何调用此函数以及可用的各种参数:
5,
"paged" => 1
"tax_query" => array(
array(
"taxonomy" => "category",
"field" => "slug",
"terms" => "videos,movies",
)
),
"orderby" => "post_date",
"order" => "DESC",
"exclude" => "1123, 4456",
"meta_key" => "",
"meta_value" => "",
"post_type" => "post",
"post_status" => "publish"
);
$posts_array = get_posts($args);
?>
There are more parameters available, but these are the most commonly used ones. Let’s look at each of these parameters:
有更多可用参数,但是这些是最常用的参数。 让我们看一下每个参数:
posts_per_page: This parameter defines the number of posts to return. Use -1 if you want all the posts.
posts_per_page :此参数定义要返回的帖子数。 如果要所有帖子,请使用-1。
paged: Allows us to navigate between a set of posts while using the posts_per_page
parameter. It is used for pagination. For example: suppose posts_per_page
is 10 and there are 20 posts in the result, then if you assign paged
to 2 then last 10 posts are returned.
paged :允许我们在使用posts_per_page
参数的同时在一组帖子之间导航。 用于分页。 例如:假设posts_per_page
为10,有20个职位的结果,那么如果您指定paged
到2然后最后10个职位被返回。
tax_query: Display posts of a particular taxonomy slug i.e., filter out posts of the other taxonomy slug. terms
can take a comma separated string representing multiple taxonomy slugs.
tax_query :显示特定分类标准段的帖子,即过滤掉其他分类标准段的帖子。 terms
可以采用逗号分隔的字符串,表示多个分类标段。
orderby: It’s used to sort the retrieved posts. Some possible values are: “none”, “date”, “rand”, “comment_count”, “meta_value”, “meta_value_num” etc. While sorting using “meta_value” and “meta_value_num” you need to provide the meta_key
parameter.
orderby :用于对检索到的帖子进行排序。 一些可能的值是:“无”,“日期”,“ rand”,“ comment_count”,“ meta_value”,“ meta_value_num”等。使用“ meta_value”和“ meta_value_num”进行排序时,您需要提供meta_key
参数。
order: Designates the ascending or descending order of the orderby
parameter. Possible values are “DESC” or “ASC”.
order :指定orderby
参数的升序或降序。 可能的值为“ DESC”或“ ASC”。
exclude: It takes a comma separated list of post IDs which will be excluded from a database search.
exclude :采用逗号分隔的帖子ID列表,该列表将从数据库搜索中排除。
meta_key and meta_value: If you provide only meta_key
, then posts which have the key will be returned. If you also provide meta_value
then posts matching the meta_value
for the meta_key
is returned.
meta_key和meta_value :如果仅提供meta_key
,则将返回具有该键的帖子。 如果还提供meta_value
则返回与meta_value
的meta_key
匹配的帖子。
post_type: Retrieves content based on post, page or custom post type. Remember that the default post_type
is only set to display posts but not pages.
post_type :根据帖子,页面或自定义帖子类型检索内容。 请记住,默认的post_type
仅设置为显示帖子,而不显示页面。
post_status: Retrieves posts by status of the post. Possible values are: “publish”, “pending”, “draft”, “future”, “any” or “trash”.
post_status :按帖子状态检索帖子。 可能的值为:“发布”,“待定”,“草稿”,“未来”,“任何”或“已删除邮件”。
WP_Post
对象 (The WP_Post
Object)The get_posts
function returns an array that contains WP_Post
objects. Here are the important properties of the WP_Post
object:
get_posts
函数返回一个包含WP_Post
对象的数组。 以下是WP_Post
对象的重要属性:
ID: ID of the post
ID :帖子的ID
post_author: Author name of the post
post_author :帖子的作者姓名
post_type: Type of the post
post_type :帖子类型
post_title: Title of the post
post_title :帖子标题
post_date: Date on which post was published. Format: 0000-00-00 00:00:00
post_date :发布帖子的日期。 格式:0000-00-00 00:00:00
post_content: Content of the post.
post_content :帖子的内容。
post_status: Status of the post
post_status :发布状态
comment_count: Number of comments for the post
comment_count :帖子的评论数
get_posts
例子 (Examples of get_posts
)Let’s check out some examples using the get_posts
function.
让我们使用get_posts
函数查看一些示例。
If you want to display the top n number of the most discussed posts on your site, then you can use get_posts
to retrieve them. Here’s an example:
如果要显示站点上讨论最多的帖子的前n个,则可以使用get_posts
进行检索。 这是一个例子:
10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "" . $post->post_title . "
";
echo "" . $post->post_content . "
";
}
?>
Here, we are using the orderby
parameter to sort the posts based on the number of comments, retrieving the top 10 posts.
在这里,我们使用orderby
参数根据评论数对帖子进行排序,以检索前10个帖子。
You can also easily retrieve random posts. This is helpful to recommend users another article on your site once they’ve finished reading the current one. Here’s the code for this:
您还可以轻松检索随机帖子。 阅读完当前文章后,建议用户在您的网站上推荐另一篇文章。 这是此代码:
1, "orderby" => "rand");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "" . $post->post_title . "
";
echo "" . $post->post_content . "
";
}
?>
In the above example, we passed the value rand
to the order_by
parameter.
在上面的示例中,我们将值rand
传递给order_by
参数。
We might want to retrieve all posts which have a particular meta key and value assigned. For example: some blogs have a reviewer for every article. We might want to retrieve articles reviewed by a particular reviewer.
我们可能要检索所有分配了特定元键和值的帖子。 例如:有些博客的每篇文章都有审稿人。 我们可能想检索由特定审稿人审阅的文章。
Here is the code to do just that:
这是执行此操作的代码:
-1, "meta_key" => "reviewer", "meta_value" = "narayanprusty");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "" . $post->post_title . "
";
echo "" . $post->post_content . "
";
}
?>
Here, we’re retrieving all the posts reviewed by “narayanprusty”. We’re assuming the reviewer name is stored via the meta key “reviewer” for every post.
在这里,我们正在检索“ narayanprusty”评论的所有帖子。 我们假设审阅者姓名是通过每个帖子的元键“ reviewer”存储的。
We may want to retrieve posts of a custom post type with a custom taxonomy name. Consider this code example:
我们可能想检索具有自定义分类名称的自定义帖子类型的帖子。 考虑以下代码示例:
-1, "post_type" => "coupons", "tax_query" => array(
array(
"taxonomy" => "coupon_category",
"field" => "slug",
"terms" => "plugins,themes",
)
));
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "" . $post->post_title . "
";
echo "" . $post->post_content . "
";
}
?>
In this example, we’re retrieving the posts of a custom post type named “coupons” which belong to the “plugins” and “themes” custom taxonomies.
在此示例中,我们检索名为“优惠券”的自定义帖子类型的帖子,这些帖子属于“插件”和“主题”自定义分类法。
In this article we saw how the get_posts
function works, the various parameters it supports, looping through the returned result and some sample use cases. The get_posts
function is one of the most used WordPress functions, I hope you can now start using it your own projects.
在本文中,我们了解了get_posts
函数的工作方式,它支持的各种参数,遍历返回的结果以及一些示例用例。 get_posts
函数是最常用的WordPress函数之一,希望您现在可以开始在自己的项目中使用它。
翻译自: https://www.sitepoint.com/exploring-the-wordpress-get_posts-function/
have_posts 修改