Home >Backend Development >PHP Tutorial >WP_Query parameters: author, search, password and permissions
So far in this series, you have learned about parameter selection that you can use with the WP_Query
class to select posts by post type, category, tags, metadata, date, status, etc. More.
In this final tutorial on WP_Query
parameters, I will cover some less commonly used parameters that can provide more flexibility in your queries.
The parameters we introduce here are used for:
Before we begin, let’s quickly review how to write parameters using WP_Query
.
When you write a WP_Query
in a theme or plugin, you need to include four main elements:
In practice, this will look something like the following:
<?php $args = array( // Arguments for your query. ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Restore original post data. wp_reset_postdata(); ?>
These parameters tell WordPress what data to get from the database, which I will cover here. So what we're focusing on here is the first part of the code:
$args = array( // Arguments for your query. );
As you can see, the parameters are contained in an array. As you follow this tutorial, you'll learn how to code them.
The parameters in the array have a specific encoding method, as follows:
$args = array( 'parameter1' => 'value', 'parameter2' => 'value', 'parameter3' => 'value' );
You must enclose parameters and their values in single quotes, use =>
between them, and separate them with commas. If you make this mistake, WordPress may not add all parameters to the query, otherwise you may see a white screen.
There are four parameters that can be used to query by author:
author
(int): Use author IDauthor_name
(String): Use "user_nicename" (not a name)author__in
(array): Use author IDauthor__not_in
(array)The first one author
allows you to query for posts by one or more authors by providing the author ID:
$args = array( 'author' => '2' );
The above code queries all posts of the author with ID 2
.
You can also use a string to query posts by multiple authors:
$args = array( 'author' => '1, 2' );
If you want to query by name, you can use author_name
Parameters:
$args = array( 'author_name' => 'rachelmccollin' );
This parameter takes the value of the user_nicename
field in the database as its parameter, which is displayed as the nickname in the user management screen:
Note that since users can edit this, it is safer to use the author
parameter if you think your users might change it.
You can also query a series of author’s posts:
$args = array( 'author__in' => array( '1', '2' ) );
The above will query the posts of two authors: the authors with IDs 1
and 2
, and the results given are the same as mine in the author
parameter.
Finally, you can exclude posts from one or more authors using the author__not_in
parameter. The following parameters query all posts except author 1
:
$args = array( 'author__not_in' => array( '1' ) );
Or you can exclude multiple authors:
$args = array( 'author__not_in' => array( '1', '2' ) );
Alternatively, you can use the author
parameter and use a minus sign in front of the author ID to exclude authors:
$args = array( 'author' => '-2' );
There is only one search parameter, which is s
. Use this to query for posts that match your search terms. For example, to query for posts containing the keyword "my favorite food" you would use:
$args = array( 's' => 'my favorite food' );
For example, you might find this useful for searching for related posts with similar keywords.
You can use two password parameters to query for posts with or without password protection:
has_password
(bool)post_password
(String)The first parameter has_password
allows you to query for posts with or without password protection. Therefore, to query password-protected posts:
$args = array( 'has_password' => true );
For posts without password:
$args = array( 'has_password' => false );
You can also query by the password itself, using post_password
Parameters:
$args = array( 'post_password' => 'mypassword' );
Permission has only one parameter, perm
, which is used to query the posts that the current user has permission to read. It takes a 'readable'
value and is intended to be used in conjunction with other parameters.
因此,要查询受密码保护的帖子并仅在用户具有适当权限时才显示它们,您可以使用以下命令:
$args = array( 'has_password' => true, 'perm' => 'readable' );
或者,如果当前用户有权查看草稿帖子,则可以使用以下命令:
$args = array( 'post_status' => 'draft', 'perm' => 'readable' );
共有三个缓存参数,它们会阻止查询检索到的数据添加到缓存中:
cache_results
(boolean):帖子信息缓存update_post_meta_cache
(boolean):发布元信息缓存update_post_term_cache
(boolean):帖子术语信息缓存这三个选项的默认值都是 true
:如果您希望将数据添加到缓存中,则不需要使用它们。
因此,要显示 product
帖子类型的所有帖子,但不将帖子信息添加到缓存中,您可以使用以下命令:
$args = array( 'post_type' => 'product', 'cache_results' => false );
通常您不应该使用这些参数,因为将发布数据添加到缓存是一个很好的做法。但是,您有时可能希望检索帖子,以便只使用一些帖子数据,在这种情况下,您不需要缓存中的其余帖子数据。例如,当您只想输出带有链接的帖子标题列表时,在这种情况下,您不需要将帖子术语数据或元数据添加到缓存中:
$args = array( 'post_type' => 'product', 'update_post_meta_cache' => false, 'update_post_term_cache' => false );
您可以使用 fields
参数来指定从查询中返回哪些字段。这可以节省从数据库中的字段返回的数据,而在循环中输出数据时不需要这些数据。
默认情况下是返回所有字段,但是您有两个选项可以使用 fields
参数来限制这一点。首先,'ids'
参数:
$args = array( 'fields' => 'ids' );
这只会返回帖子 ID 数组,不会返回其他字段。如果您想在循环中输出任何内容(例如帖子标题),则必须使用 get_the_title ($post->ID);
等函数来输出标题,这将处理事情的方式很冗长。
您可以使用的另一个参数获取帖子 ID 与子帖子 ID 的关联数组:
$args = array( 'fields' => 'id=>parent' );
您可以使用它根据您的其他参数及其子参数查询帖子。
WP_Query
系列的这一部分介绍了 WP_Query
类的最终参数集。您可以使用它们按作者、密码保护状态或密码本身和搜索词查询帖子,并设置是否将查询结果添加到缓存以及查询返回哪些字段。
在本系列的下一部分中,您将看到一些在主题或插件中使用 WP_Query
的有效示例。
The above is the detailed content of WP_Query parameters: author, search, password and permissions. For more information, please follow other related articles on the PHP Chinese website!