如何获取具有任何帖子状态的所有帖子?
-
-
您是否尝试过使用[`post_status`参数](http://codex.wordpress.org/Function_Reference/WP_Query#Type_.26_Status_Parameters),即.`'post_status'=>'任何'`?Have you tried using the [`post_status` parameter](http://codex.wordpress.org/Function_Reference/WP_Query#Type_.26_Status_Parameters), ie. `'post_status' => 'any'`?
- 5
- 2011-03-30
- t31os
-
我强烈建议使用" WP_Query","pre_get_posts"或"get_posts"而不是" query_posts".永远不要使用`query_posts`I ***strongly*** recommend using `WP_Query` `pre_get_posts` or `get_posts` instead of `query_posts`. Never use `query_posts`
- 2
- 2013-04-16
- Tom J Nowell
-
@TomJNowell:那是回溯了:)我现在最常使用WP_Query.@TomJNowell: that was way back :) I use WP_Query most ofter now..
- 0
- 2013-04-17
- Sisir
-
@Sisir小心点,将`WP_Query`用于前端,将`get_posts`用于管理员查询,因为`wp_reset_postdata`存在问题(请参阅[note](https://codex.wordpress.org/Class_Reference/WP_Query#Interacting_with_WP_Query)和[ticket](https://core.trac.wordpress.org/ticket/18408).@Sisir be careful, use `WP_Query` for front-end, and `get_posts` for admin queries as there is an issue with `wp_reset_postdata` (see the [note](https://codex.wordpress.org/Class_Reference/WP_Query#Interacting_with_WP_Query) and [ticket](https://core.trac.wordpress.org/ticket/18408) on this issue).
- 1
- 2017-01-30
- Aurovrata
-
5 个回答
- 投票数
-
- 2011-03-30
您可以使用post_status参数:
* 'publish' - a published post or page * 'pending' - post is pending review * 'draft' - a post in draft status * 'auto-draft' - a newly created post, with no content * 'future' - a post to publish in the future * 'private' - not visible to users who are not logged in * 'inherit' - a revision. see get_children. * 'trash' - post is in trashbin. added with Version 2.9.
我不确定它是否接受" any",因此请使用具有所有所需状态的数组:
$args = array( 'post_type' => 'my-post-type', 'post_author' => $current_user->ID, 'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash') ); $query = new WP_Query($args); while ( $query->have_posts() ) : $query->the_post();
You can use the post_status parameter:
* 'publish' - a published post or page * 'pending' - post is pending review * 'draft' - a post in draft status * 'auto-draft' - a newly created post, with no content * 'future' - a post to publish in the future * 'private' - not visible to users who are not logged in * 'inherit' - a revision. see get_children. * 'trash' - post is in trashbin. added with Version 2.9.
I'm not sure that it accepts 'any' so use an array with all of the statuses you want:
$args = array( 'post_type' => 'my-post-type', 'post_author' => $current_user->ID, 'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash') ); $query = new WP_Query($args); while ( $query->have_posts() ) : $query->the_post();
-
您也可以使用get_post_stati()获取所有状态,包括自定义状态.You could also use `get_post_stati()` to get all statuses, including custom ones.
- 8
- 2013-01-31
- fuxia
-
浪费机会杀死`query_posts`呼叫...A wasted opportunity to kill off a `query_posts` call...
- 5
- 2013-04-16
- Tom J Nowell
-
太糟糕了,我们不能做这样的事情''post_status'=> array('!inherit');`(表示除继承以外的任何post_status)too bad we can't do something like this `'post_status' => array( '!inherit' );` (to indicate any post_status other than inherit)
- 0
- 2017-01-03
- aequalsb
-
@aequalsb''post_status'=> array_diff(get_post_stati(),['inherit']);那怎么办?@aequalsb what about `'post_status' => array_diff(get_post_stati(), ['inherit']);`
- 0
- 2018-10-29
- Cheslab
-
题外话.实际上,"任何"都是真实的东西.文件:https://developer.wordpress.org/reference/classes/wp_query/#post-type-parametersoff-topic. 'any' is a real thing actually. Docs: https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
- 2
- 2020-01-20
- kirillrocks
-
- 2013-01-31
有一种简单的方法来获取具有任何状态的所有帖子:
$articles = get_posts( array( 'numberposts' => -1, 'post_status' => 'any', 'post_type' => get_post_types('', 'names'), ) );
现在您可以遍历所有帖子:
foreach ($articles as $article) { echo $article->ID . PHP_EOL; //... }
There is simple way, how to get all posts with any status:
$articles = get_posts( array( 'numberposts' => -1, 'post_status' => 'any', 'post_type' => get_post_types('', 'names'), ) );
Now you can iterate throughout all posts:
foreach ($articles as $article) { echo $article->ID . PHP_EOL; //... }
-
** $posts和$post与Wordpress自己的变量名冲突**.如果您使用此代码将除主(主要内容)div之外的内容放入其他内容,这将覆盖main中显示的内容.如果您的目的确实是要完全替换原始查询结果,那么这当然是您想要的.但是重命名$posts和$post变量仍然是一个好主意.**$posts and $post conflict with Wordpress' own variable names**. If you are using this code to put something in other than the primary (main content) div, this will overwrite what would have been shown in main. If your intention really is to completely replace the original query results, this is what you want, of course. But it's still a good idea to rename the $posts and $post variables.
- 2
- 2014-02-03
- Henrik Erlandsson
-
@Henrik我根本不打算减少您的评论(您的逻辑是合理且安全的),但是我认为在函数内部使用$post/$posts是完全可以接受的,而无需访问全局$post/$posts变量-因为它有助于我在开发过程中保持逻辑.@Henrik i am not intending to diminish your comment at all (your logic is sound and safe), but i consider using $post/$posts as perfectly acceptable inside a function without access to the global $post/$posts variables -- because it helps me maintain logic during development.
- 5
- 2017-01-03
- aequalsb
-
- 2012-10-05
WP_Query
类方法->query()
接受any
的post_status
参数.请参见wp_get_associated_nav_menu_items()
以获取证明.get_posts()
(上面的调用只是一个包装器)也是如此.The
WP_Query
class method->query()
accepts anany
argument forpost_status
. Seewp_get_associated_nav_menu_items()
for a proof.The same goes for
get_posts()
(which is just a wrapper for above call).-
从WP_Query文档中:_'any'-从"exclude_from_search"设置为true的帖子类型中检索任何状态._(这里有一个错字,它们实际上是帖子状态,而不是帖子类型.)这表示状态为"自动-草稿和垃圾箱除外.From the WP_Query docs: _'any' - retrieves any status except those from post types with 'exclude_from_search' set to true._ (There's a typo there, they actually mean post statuses instead of post types.) This means statuses `auto-draft` and `trash` are excluded.
- 4
- 2013-04-15
- Tamlyn
-
@Tamlyn Afaik,这不是错字.它从帖子类型中检索公开的所有状态.状态只是条款.他们自己没有_public_或_private_属性.您可以通过禁用query_var ...来禁用分类法,无论出于何种原因.旁注:[职位状态的复数是...](http://unserkaiser.com/uncategorized/status-and-plural/).@Tamlyn Afaik, this is no typo. It _retrieves any status from post types_ that are publicly available. Status are just terms. They got no _public_ or _private_ property themselves. You _could_ disable a taxonomy with disabling the `query_var`... for whatever reason one would do that. Sidenote: [The plural of post status is...](http://unserkaiser.com/uncategorized/status-and-plural/).
- 0
- 2013-04-15
- kaiser
-
如果您跟踪代码(通常比阅读文档容易,我会发现),您会发现`WP_Query#get_posts()`会调用`get_post_stati()`来过滤$ wp_post_statuses`以获取`exclude_from_search`为真的值.从查询中排除具有这些[状态](https://www.google.com/search?q=define+statuses)的帖子.当post_type设置为" any"时,对于帖子类型也有类似的过程.If you trace through the code (often easier than reading the docs, I find) you can see that `WP_Query#get_posts()` calls `get_post_stati()` which filters `$wp_post_statuses` for values where `exclude_from_search` is true then it excludes posts with these [statuses](https://www.google.com/search?q=define+statuses) from the query. There's a similar process for post types when post_type is set to 'any'.
- 1
- 2013-04-16
- Tamlyn
-
@Tamlyn在检查$ wp_post_statuses属性的内容之后,我不得不承认你是正确的:)@Tamlyn After checking the contents of the `$wp_post_statuses` property, I have to admit that you're right :)
- 0
- 2013-04-16
- kaiser
-
不适用于垃圾箱状态.doesn't work for trash status.
- 0
- 2018-12-10
- Maxwell s.c
-
- 2019-08-28
在大多数情况下,您可以将
get_posts()
与'any'
参数一起使用:$posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'any', 'post_type' => 'my-post-type', ) );
但是,这样一来,您将不会收到状态为
trash
和auto-draft
的帖子.您需要明确提供它们,就像这样:$posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'any, trash, auto-draft', 'post_type' => 'my-post-type', ) );
或者您可以使用get_post_stati()函数显式提供所有现有状态:
$posts = get_posts( array( 'numberposts' => -1, 'post_status' => get_post_stati(), 'post_type' => 'my-post-type', ) );
In most cases you can use
get_posts()
with'any'
parameter for this:$posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'any', 'post_type' => 'my-post-type', ) );
But this way you won't get posts with status
trash
andauto-draft
. You need to provide them explicitly, like this:$posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'any, trash, auto-draft', 'post_type' => 'my-post-type', ) );
Or you can use get_post_stati() function to provide all existing statuses explicitly:
$posts = get_posts( array( 'numberposts' => -1, 'post_status' => get_post_stati(), 'post_type' => 'my-post-type', ) );
-
- 2019-03-28
即使您将
any
作为post_status
传递,您如果所有以下条件都成立,仍然不会在结果中显示帖子:- 正在查询单个帖子.例如,按
name
(即子弹)查询. - 帖子的发布状态为非公开.
- 客户端没有活动的管理会话,即您当前尚未登录.
解决方案
显式查询 每个状态.例如,要查询不是
trash
或auto-draft
的stati(您不太可能想要这些),可以执行以下操作:$q = new WP_Query([ /* ... */ 'post_status' => get_post_stati(['exclude_from_search' => false]), ]);
Even if you pass
any
aspost_status
, you still will not get the post in the result if all of the following conditions are true:- A single post is being queried. An example of this would be querying by
name
, i.e. the slug. - The post has a post status that is not public.
- The client does not have an active admin session, i.e. you are not currently logged in.
Solution
Query explicitly for every status. For example, to query for stati which are not
trash
orauto-draft
(it's pretty unlikely that you want those), you could do something like this:$q = new WP_Query([ /* ... */ 'post_status' => get_post_stati(['exclude_from_search' => false]), ]);
我正在创建一个前端仪表板,其中需要显示当前用户的所有帖子.因此,我需要显示所有状态下的帖子,主要是
published
,trashed
和pending
.我现在使用一个简单的查询,但它只返回已发布的帖子.有人可以帮忙吗?我还需要做什么?