1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > have_posts 修改_探索WordPress的get_posts函数

have_posts 修改_探索WordPress的get_posts函数

时间:2023-06-15 16:25:06

相关推荐

have_posts 修改_探索WordPress的get_posts函数

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 theget_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 theWP_Queryobject andget_pagesfunction.

在本文中,我们将通过一些示例说明如何在自己的项目中使用get_posts()函数。 我们还将介绍此函数的一些典型用例,以及与WP_Query对象和get_pages函数的区别。

什么是get_posts()函数? (What is theget_posts()Function?)

Theget_postsfunction 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核心中可用。 该功能基本上用于通过应用自定义过滤器并基于一组参数对最终结果进行排序来从数据库检索帖子。

Theget_posts()function returns an array ofWP_Postobjects. EachWP_Postobject represents an individual post.

get_posts()函数返回WP_Post对象的数组。 每个WP_Post对象代表一个单独的帖子。

Internallyget_postsuses theWP_Queryobject 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 theWP_QueryObject Directly?)

Plugins useget_postsfunction instead ofWP_Queryobject because using theWP_Queryobject directly alters the main loop (i.e., the global$wp_queryvariable) which would cause site issues.

插件使用get_posts函数而不是WP_Query对象,因为使用WP_Query对象会直接更改主循环(即全局$wp_query变量),这将导致站点问题。

get_posts()get_pages()函数之间有什么区别? (What Is the Difference Betweenget_posts()andget_pages()Functions?)

Both of them are used to retrieve posts from the WordPress database, however, here are some of the differences between them:

两者都用于从WordPress数据库中检索帖子,但是,这是它们之间的一些区别:

Several of the parameter names and values differ between them. Although they behave the same way regardless of the names of the parameters.几个参数名称和值之间有所不同。 尽管无论参数名称如何,它们的行为都相同。

Theget_pages()function currently does not retrieve posts based onmeta_keyandmeta_valueparameters.

get_pages()函数当前不基于meta_keymeta_value参数检索帖子。

Theget_pages()function doesn’t use theWP_Queryobject. Instead, it constructs and executes SQL queries directly.

get_pages()函数不使用WP_Query对象。 而是直接构造和执行SQL查询。

get_posts()函数参数 (get_posts()Function Parameters)

Theget_postsfunction 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:

这是一个代码示例,显示了如何调用此函数以及可用的各种参数:

<?php $args = array("posts_per_page" => 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 theposts_per_pageparameter. It is used for pagination. For example: supposeposts_per_pageis 10 and there are 20 posts in the result, then if you assignpagedto 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.termscan 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 themeta_keyparameter.

orderby:用于对检索到的帖子进行排序。 一些可能的值是:“无”,“日期”,“ rand”,“ comment_count”,“ meta_value”,“ meta_value_num”等。使用“ meta_value”和“ meta_value_num”进行排序时,您需要提供meta_key参数。

order: Designates the ascending or descending order of theorderbyparameter. 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_keyandmeta_value: If you provide onlymeta_key, then posts which have the key will be returned. If you also providemeta_valuethen posts matching themeta_valuefor themeta_keyis returned.

meta_keymeta_value:如果仅提供meta_key,则将返回具有该键的帖子。 如果还提供meta_value则返回与meta_valuemeta_key匹配的帖子。

post_type: Retrieves content based on post, page or custom post type. Remember that the defaultpost_typeis 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对象 (TheWP_PostObject)

Theget_postsfunction returns an array that containsWP_Postobjects. Here are the important properties of theWP_Postobject:

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 ofget_posts)

Let’s check out some examples using theget_postsfunction.

让我们使用get_posts函数查看一些示例。

最受欢迎的帖子 (Most Popular Posts)

If you want to display the top n number of the most discussed posts on your site, then you can useget_poststo retrieve them. Here’s an example:

如果要显示站点上讨论最多的帖子的前n个,则可以使用get_posts进行检索。 这是一个例子:

<?php$args = array("posts_per_page" => 10, "orderby" => "comment_count");$posts_array = get_posts($args);foreach($posts_array as $post){echo "<h1>" . $post->post_title . "</h1><br>";echo "<p>" . $post->post_content . "</p><br>";} ?>

Here, we are using theorderbyparameter to sort the posts based on the number of comments, retrieving the top 10 posts.

在这里,我们使用orderby参数根据评论数对帖子进行排序,以检索前10个帖子。

随机帖子 (Random Posts)

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:

您还可以轻松检索随机帖子。 阅读完当前文章后,建议用户在您的网站上推荐另一篇文章。 这是此代码:

<?php$args = array("posts_per_page" => 1, "orderby" => "rand");$posts_array = get_posts($args);foreach($posts_array as $post){echo "<h1>" . $post->post_title . "</h1><br>";echo "<p>" . $post->post_content . "</p><br>";} ?>

In the above example, we passed the valuerandto theorder_byparameter.

在上面的示例中,我们将值rand传递给order_by参数。

具有相匹配的元键和值的帖子 (Posts with Matching Meta Key and Value)

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:

这是执行此操作的代码:

<?php$args = array("posts_per_page" => -1, "meta_key" => "reviewer", "meta_value" = "narayanprusty");$posts_array = get_posts($args);foreach($posts_array as $post){echo "<h1>" . $post->post_title . "</h1><br>";echo "<p>" . $post->post_content . "</p><br>";} ?>

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”存储的。

具有自定义分类的自定义帖子类型 (Custom Post Type with Custom Taxonomy)

We may want to retrieve posts of a custom post type with a custom taxonomy name. Consider this code example:

我们可能想检索具有自定义分类名称的自定义帖子类型的帖子。 考虑以下代码示例:

<?php$args = array("posts_per_page" => -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 "<h1>" . $post->post_title . "</h1><br>";echo "<p>" . $post->post_content . "</p><br>";} ?>

In this example, we’re retrieving the posts of a custom post type named “coupons” which belong to the “plugins” and “themes” custom taxonomies.

在此示例中,我们检索名为“优惠券”的自定义帖子类型的帖子,这些帖子属于“插件”和“主题”自定义分类法。

结论 (Conclusion)

In this article we saw how theget_postsfunction works, the various parameters it supports, looping through the returned result and some sample use cases. Theget_postsfunction is one of the most used WordPress functions, I hope you can now start using it your own projects.

在本文中,我们了解了get_posts函数的工作方式,它支持的各种参数,遍历返回的结果以及一些示例用例。get_posts函数是最常用的WordPress函数之一,希望您现在可以开始在自己的项目中使用它。

翻译自: /exploring-the-wordpress-get_posts-function/

have_posts 修改

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。