This article will outline what you need to know about WordPress and its filtering mechanism available to you via your posts administration screen.
本文将概述您需要了解的有关WordPress及其过滤机制(可通过帖子管理屏幕)的知识。
We will detail exactly what the WordPress filters are and the purpose they serve in displaying your post listings. From there we will learn how you can extend your post administration area – by creating new customized WordPress administration filters.
我们将详细介绍WordPress过滤器的确切含义以及它们在显示帖子列表中的作用。 从那里我们将学习如何通过创建新的自定义WordPress管理过滤器来扩展您的帖子管理区域。
By the end you will be able to take what you have learned and apply it to your own projects, allowing you to provide additional information and functionality for your end users.
到最后,您将能够掌握所学到的知识并将其应用于自己的项目,从而为最终用户提供其他信息和功能。
筛选帖子 (Filtering Posts)
WordPress will output its default filter mechanisms to the posts administration screen. These filters allow you to display only certain posts that match set criteria. For example, you may want to display posts published within a set date period (as shown below).
WordPress将其默认过滤器机制输出到帖子管理屏幕。 这些过滤器允许您仅显示符合设置条件的某些帖子。 例如,您可能要显示在指定日期范围内发布的帖子(如下所示)。
WordPress comes pre-configured with several filters that you can use on your posts administration screen. Additional filters may be added by certain themes and plugins and provide additional filters for you to use.
WordPress预先配置了几个过滤器,您可以在帖子管理屏幕上使用它们。 某些主题和插件可能会添加其他过滤器,并提供其他过滤器供您使用。
Overall these filters serve the same purpose of allowing you to narrow down your listing of posts based on a set of criteria, viewing only the posts that are relevant given your set criteria.
总体而言,这些过滤器具有相同的目的,即允许您根据一组条件缩小帖子列表的范围,仅查看与给定条件相关的帖子。
添加自己的过滤器 (Adding Your Own Filters)
While WordPress comes with its own great set of filters, often you may want to add your own to help your users (or yourself) in narrowing down posts on your administration screen.
尽管WordPress拥有自己的一套强大的过滤器,但您通常可能希望添加自己的过滤器,以帮助您的用户(或您自己)缩小管理屏幕上的帖子。
You can add additional filters to your website via the use of two hooks
您可以使用两个挂钩将其他过滤器添加到您的网站
restrict_manage_posts
restrict_manage_posts
pre_get_posts
pre_get_posts
These two hooks, when combined will allow you to filter your posts by your chosen criteria and return only posts / items that match that criteria.
结合使用这两个钩子后,您就可以根据所选条件过滤帖子,并且仅返回符合该条件的帖子/项目。
Before we go through and explore these hooks, lets talk about a real world example (so that when we start building our filters you can see how they may be useful).
在研究和探讨这些挂钩之前,让我们先讨论一个真实的示例(这样,当我们开始构建过滤器时,您就可以了解它们的用处)。
Imagine that we are building a self managed website where each post
will be manually assigned a post format
and automatically assigned a post author
想象一下,我们正在建立一个自我管理的网站,将为每个post
手动分配一个post format
并为他们自动分配一个post author
When we are on our administration listing page for our posts it will show the user all posts, regardless of what format the post is in or the author who wrote it. This screen can be messy and hard to read through (and often will require flipping through pages of records to find what you need)
当我们在我们的帖子管理列表页面上时,它将向用户显示所有帖子,无论帖子采用哪种格式或撰写者。 该屏幕可能混乱且难以阅读(通常需要翻阅记录页面才能找到所需的内容)
Even though a post will have link with the name of the author, it is possible the user will simply overlook this (along with the small icon displayed that represents the post format type of the post).
即使帖子中包含作者姓名的链接,用户也有可能会忽略它(显示的小图标代表帖子的帖子格式类型)。
It is more intuitive to add drop downs to the top of each administration listing screen that implicitly show that your posts will be filtered. This is what we will be doing in this tutorial.
将下拉列表添加到每个管理列表屏幕的顶部会更加直观,这些下拉菜单隐式显示您的帖子将被过滤。 这就是我们在本教程中要做的。
使用“ restrict_manage_posts”过滤器创建选择下拉列表 (Creating the Select Drop Down Using the ‘restrict_manage_posts’ Filter)
The first thing that needs to be done is the creation of the select drop down for the two additional filters we will be making.
首先需要做的是为我们将要创建的另外两个过滤器创建选择下拉列表。
You will need to navigate over to your child themes functions.php
file (or another applicable file) and add code which will add these two new filters to your posts administration screen.
您将需要导航至子主题functions.php
文件(或另一个适用的文件),并添加代码,这会将这两个新过滤器添加到帖子管理屏幕。
按作者过滤 (Filtering by Author)
//defining the filter that will be used to select posts by 'post formats'
function add_post_formats_filter_to_post_administration(){
//execute only on the 'post' content type
global $post_type;
if($post_type == 'post'){
$post_formats_args = array(
'show_option_all' => 'All Post formats',
'orderby' => 'NAME',
'order' => 'ASC',
'name' => 'post_format_admin_filter',
'taxonomy' => 'post_format'
);
//if we have a post format already selected, ensure that its value is set to be selected
if(isset($_GET['post_format_admin_filter'])){
$post_formats_args['selected'] = sanitize_text_field($_GET['post_format_admin_filter']);
}
wp_dropdown_categories($post_formats_args);
}
}
add_action('restrict_manage_posts','add_post_formats_filter_to_post_administration');
Lets step though this code so that you can get an understanding of exactly what we are doing.
让我们逐步看一下这段代码,以便您可以准确了解我们在做什么。
We create a function and attach it to the
restrict_manage_posts
action. This gives us access to the filter zone shown at the top of the posts administration page.我们创建一个函数并将其附加到
restrict_manage_posts
操作。 这使我们可以访问帖子管理页面顶部显示的过滤器区域。We get the current post type listing being displayed via the global
post_type
. We use this to determine if we are on the correct post type (we only want to execute this for posts, not pages or other content types).我们通过全局
post_type
获取当前显示的帖子类型列表。 我们使用它来确定我们是否使用正确的帖子类型(我们只想对帖子执行此操作,而不是页面或其他内容类型)。We intent to call the
wp_dropdown_categories
WordPress function which will generate a drop down list of all post formats based on a set of criteria. Post formats are actually just one of WordPress’s custom taxonomies (much like categories or tags) and as such we can use this function by supplying a list of arguments.我们打算调用
wp_dropdown_categories
WordPress函数,该函数将基于一组条件生成所有帖子格式的下拉列表。 帖子格式实际上只是WordPress的自定义分类法之一(非常类似于类别或标签),因此我们可以通过提供参数列表来使用此功能。show_option_all
– This determines the name that will be shown when we do not want to filter anything (we use this so that you can show ‘all post formats’ and will see all of your post formats).show_option_all
–这确定了当我们不想过滤任何内容时将显示的名称(我们使用它是为了使您可以显示“所有文章格式”并看到所有文章格式)。orderby
– This is how this will be ordered, I have chosen to order this by its name.orderby
–这是订购方式,我选择按其名称订购。order
– This determines which way the list will be sorted, I have chosen ascending.order
–这决定了列表的排序方式,我选择了升序。name
– This is the name of the drop down list itself. You need to give this a unique name so that you can fetch its chosen value later on.name
–这是下拉列表本身的名称。 您需要给它一个唯一的名称,以便以后可以获取其选择的值。taxonomy
– (This is important) This value determines which elements will be pulled into the list. Since post formats are just a taxonomy we can specify its taxonomy name ofpost_format
and it will collect the post format terms.taxonomy
-(这很重要)此值确定将哪些元素拉入列表。 由于发布格式只是一种分类法,因此我们可以将其分类名称指定为post_format
,它将收集发布格式条款。
We intent to call the
wp_dropdown_categories
WordPress function which will generate a drop down list of all post formats based on a set of criteria. Post formats are actually just one of WordPress’s custom taxonomies (much like categories or tags) and as such we can use this function by supplying a list of arguments.我们打算调用
wp_dropdown_categories
WordPress函数,该函数将基于一组条件生成所有帖子格式的下拉列表。 帖子格式实际上只是WordPress的自定义分类法之一(非常类似于类别或标签),因此我们可以通过提供参数列表来使用此功能。After we declare our arguments for the
wp_dropdown_categories
function, we search the global$_GET
variable to see if we have actually already chosen a post format type to filter by. If a value exists we need to add a new argument onto our argument array calledselected
which basically tells the drop down list which value should be chosen by default.在声明了
wp_dropdown_categories
函数的参数之后,我们搜索全局$_GET
变量以查看是否实际上已经选择了一种邮寄格式类型作为过滤依据。 如果存在值,则需要在参数数组中添加一个名为selected
的新参数,该参数基本上告诉下拉列表默认情况下应选择哪个值。
按作者过滤 (Filtering by Author)
//defining the filter that will be used so we can select posts by 'author'
function add_author_filter_to_posts_administration(){
//execute only on the 'post' content type
global $post_type;
if($post_type == 'post'){
//get a listing of all users that are 'author' or above
$user_args = array(
'show_option_all' => 'All Users',
'orderby' => 'display_name',
'order' => 'ASC',
'name' => 'aurthor_admin_filter',
'who' => 'authors',
'include_selected' => true
);
//determine if we have selected a user to be filtered by already
if(isset($_GET['aurthor_admin_filter'])){
//set the selected value to the value of the author
$user_args['selected'] = (int)sanitize_text_field($_GET['aurthor_admin_filter']);
}
//display the users as a drop down
wp_dropdown_users($user_args);
}
}
add_action('restrict_manage_posts','add_author_filter_to_posts_administration');
Lets step through this code so you can get an understanding of exactly what we are doing
让我们逐步看一下这段代码,以便您可以确切了解我们在做什么
We create a function and attach it to the
restrict_manage_posts
action. This will give us access to the filters at the top of the posts administration screen.我们创建一个函数并将其附加到
restrict_manage_posts
操作。 这将使我们能够访问帖子管理屏幕顶部的过滤器。We use the global
post_type
variable to check to see if we are currently on the correct posts administration screen. We only want to execute onposts
.我们使用全局
post_type
变量检查当前是否在正确的帖子管理屏幕上。 我们只想对posts
执行。We want to use the
wp_dropdown_users
function to generate a listing of users on the website. We need to supply the following arguments to it.我们想使用
wp_dropdown_users
函数生成网站上的用户列表。 我们需要为其提供以下参数。show_option_all
– This determines the default option for the select list. In our case we want it to be ‘All Users’ which would mean we don’t want to filter anything at all.show_option_all
–这确定选择列表的默认选项。 在我们的情况下,我们希望它是“所有用户”,这意味着我们根本不想过滤任何内容。orderby
– Determines how the list will be ordered. I have chosen to order just by the name of the author.orderby
–确定列表的排序方式。 我选择按作者的名字来订购。order
– Determines in what order the list will be ordered, I have chosen ascending order.order
–确定列表的排序顺序,我选择了升序。name
– The name of the drop down, which will be used later on when we need to fetch the chosen value so we can filter the posts.name
–下拉列表的名称,稍后将在我们需要获取所选值时使用,以便我们可以过滤帖子。who
– Determines who will be chosen. The only value we can set isauthors
which will fetch all users who can create posts.who
–确定将选择谁。 我们可以设置的唯一值是authors
,它将提取所有可以创建帖子的用户。include_selected
– I have chosen to enable this for simplicities sake.include_selected
–为简单起见,我选择启用此功能。
After we declare our arguments for the
wp_dropdown_users
function, we check the global$_GET
to see if we have the drop down value set. If this is set it means that we are already filtering by a user and we need to update the arguments. We set the value ofselected
to the ID of the author which ensures that the author is selected by default when the page loads.在声明
wp_dropdown_users
函数的参数后,我们检查全局$_GET
以查看是否设置了下拉值。 如果设置了该参数,则意味着我们已经被用户过滤,并且需要更新参数。 我们将selected
的值设置为作者的ID,以确保在页面加载时默认选择作者。
After adding both of these filters your posts administration screen should have an additional set of drop down lists. One for your post formats and another for your authors as seen below
添加这两个过滤器后,您的帖子管理屏幕应具有其他一组下拉列表。 一种用于您的帖子格式,另一种用于您的作者,如下所示
使用“ pre_get_posts”过滤器过滤帖子 (Filtering the Posts Using the ‘pre_get_posts’ Filter)
Now that we have an interface in which the user can select their post format and / or authors, we need to filter the listing of posts based on these drop downs.
现在我们有了一个界面,用户可以在其中选择他们的帖子格式和/或作者,我们需要根据这些下拉列表过滤帖子列表。
We will do this by hooking into the pre_get_posts
filter.
我们将通过连接到pre_get_posts
过滤器来做到这一点。
The pre_get_posts
filter has access to the current query
before it is executed by WordPress and will let us change the posts that will be fetched from the database.
pre_get_posts
过滤器可以在WordPress执行之前访问当前query
,这将使我们更改将从数据库中获取的帖子。
The overall idea is to modify the query
in such as way as to restrict which posts will be brought into the administration listing. To do this for both post formats and authors is slightly different so I have broken down each method below
总体思路是修改query
,以限制将哪些帖子带入管理列表中。 对于帖子格式和作者,这样做都略有不同,因此我在下面细分了每种方法
筛选帖子格式 (Filtering for Post Formats)
//restrict the posts by the chosen post format
function add_post_format_filter_to_posts($query){
global $post_type, $pagenow;
//if we are currently on the edit screen of the post type listings
if($pagenow == 'edit.php' && $post_type == 'post'){
if(isset($_GET['post_format_admin_filter'])){
//get the desired post format
$post_format = sanitize_text_field($_GET['post_format_admin_filter']);
//if the post format is not 0 (which means all)
if($post_format != 0){
$query->query_vars['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'field' => 'ID',
'terms' => array($post_format)
)
);
}
}
}
}
add_action('pre_get_posts','add_post_format_filter_to_posts');
Let us break this code down and run through exactly what it is we are doing:
让我们分解一下这段代码,并准确地进行我们正在做的事情:
We create a function called
add_post_format_filter_to_posts
and hook it onto thepre_get_posts
filter. This filter has access to the globalquery
variable so we also pass that into our function so we can manipulate it. This variable is passed by reference so we don’t need to return or echo any values, any changes to the query will stay.我们创建了一个名为
add_post_format_filter_to_posts
的函数,并将其挂接到pre_get_posts
过滤器上。 该过滤器可以访问全局query
变量,因此我们也将其传递给函数,以便我们可以对其进行操作。 该变量是通过引用传递的,因此我们无需返回或回显任何值,对查询的任何更改都将保留。We get the global
post_type
andpage_now
variables and check to seepage_now
is equal toedit.php
andpost_type
is equal topost
. Essentially we are checking to make sure we are on the post administration screen.我们获取全局
post_type
和page_now
变量,并检查page_now
等于edit.php
,post_type
等于post
。 从本质上讲,我们正在检查以确保我们处于后期管理屏幕上。We determine if a post format has been selected by checking the
$_GET
variable. If it has been set, we collect its value.我们通过检查
$_GET
变量来确定是否选择了帖子格式。 如果已设置,我们将收集其值。We compare the chosen value to ensure it is not our default value of 0 (default meaning it wants to show all post formats, which is the exact same as not applying a post format filter). As long as we have chosen that we want to filter down to a specific post format, we create modify the
query
object so that we can tell it only to select only posts matching our chosen post format. certain posts.我们比较所选值,以确保它不是我们的默认值0(默认值表示它要显示所有帖子格式,这与不应用帖子格式过滤器完全相同)。 只要我们选择要过滤为特定的帖子格式,就可以创建修改
query
对象,这样我们就可以告诉它仅选择与所选帖子格式匹配的帖子。 某些职位。We modify the
query
and change itstax_query
element so that it filters by post formats. We set thetaxonomy
topost_format
, thefields
toID
and then theincludes
to an array of$post_format
(which contains our value we want to filter by).我们修改
query
并更改其tax_query
元素,以便按帖子格式进行过滤。 我们将taxonomy
设置为post_format
,将fields
为ID
,然后将includes
设置为$post_format
数组(其中包含我们要过滤的值)。
Once you have this code you will be able to filter your posts based on their post format. For example you may want to filter your posts to only show quotes
or chats
as shown below:
获得此代码后,您将能够根据帖子的格式过滤帖子。 例如,您可能希望过滤帖子以仅显示quotes
或chats
,如下所示:
按作者过滤 (Filtering by Author)
//restrict the posts by an additional author filter
function add_author_filter_to_posts_query($query){
global $post_type, $pagenow;
//if we are currently on the edit screen of the post type listings
if($pagenow == 'edit.php' && $post_type == 'post'){
if(isset($_GET['aurthor_admin_filter'])){
//set the query variable for 'author' to the desired value
$author_id = sanitize_text_field($_GET['aurthor_admin_filter']);
//if the author is not 0 (meaning all)
if($author_id != 0){
$query->query_vars['author'] = $author_id;
}
}
}
}
add_action('pre_get_posts','add_author_filter_to_posts_query');
Let us break this code down and run through exactly what it is we are doing
让我们分解这段代码,并准确地进行我们正在做的事情
We create a function called
add_author_filter_to_posts_query
and hook it onto thepre_get_posts
filter. This filter has access to the globalquery
variable so we also pass that into our function so we can manipulate it. This variable is passed by reference so we don’t need to return or echo any values, any changes to the query will stay.我们创建了一个名为
add_author_filter_to_posts_query
的函数,并将其连接到pre_get_posts
过滤器上。 该过滤器可以访问全局query
变量,因此我们也将其传递给函数,以便我们可以对其进行操作。 该变量是通过引用传递的,因此我们无需返回或回显任何值,对查询的任何更改都将保留。We get the global
post_type
andpage_now
variables and check to seepage_now
is equal toedit.php
andpost_type
is equal topost
. Essentially we are checking to make sure we are on the post administration screen.我们获取全局
post_type
和page_now
变量,并检查page_now
等于edit.php
,post_type
等于post
。 从本质上讲,我们正在检查以确保我们处于后期管理屏幕上。We determine if a post format has been selected by checking the
$_GET
variable. If it has been set, we collect its value.我们通过检查
$_GET
变量来确定是否选择了帖子格式。 如果已设置,我们将收集其值。We check to make sure that the entered value is not
0
as that is our default value. A value of0
represents the drop down filter option of ‘All Authors’ which would essentially mean that our filter does nothing. If our value is not0
we access thequery_vas
inside thequery
and set the value ofauthor
to our ID. Once this is set it will pull posts belonging only to that author id.我们检查以确保输入的值不为
0
因为这是我们的默认值。 值为0
表示“所有作者”的下拉过滤器选项,这实际上意味着我们的过滤器不执行任何操作。 如果我们的值不为0
则访问query
内部的query_vas
并将author
的值设置为ID。 设置好后,它将拉出仅属于该作者ID的帖子。
Once you have this code up and running you will be able to filter your posts by their author.
启动并运行此代码后,您将可以按其作者过滤帖子。
In my example site, I have two users admin user
and contributor user
. I can now choose to see posts only from a selected author as seen below:
在示例站点中,我有两个用户admin user
和contributor user
。 我现在可以选择仅查看来自选定作者的帖子,如下所示:
结论 (In Conclusion)
Now that you have a better understanding of WordPress administrative filters, you can use this tutorial and apply it to your own projects.
现在您对WordPress管理过滤器有了更好的了解,您可以使用本教程并将其应用于您自己的项目。
There are a wide range of already provided post attributes that you can filter by. In this tutorial we looked at filtering by the value of author
and the taxonomy post_format
, but you could easily filter by other values such as the ones listed on the WordPress Query class reference page.
您可以过滤多种已经提供的帖子属性。 在本教程中,我们研究了按author
值和分类法post_format
过滤,但是您可以轻松地通过其他值进行过滤,例如WordPress Query类参考页面上列出的值。
翻译自: https://www.sitepoint.com/customized-wordpress-administration-filters/