对于任何post类型,Wordpress get_posts总是返回null,post_status=all等



我在这里很头疼,无法在简单的查询中返回任何帖子:

get_posts();
get_posts([
'post_type' => 'meetings',
'post_status' => 'any'
]);
$posts = get_posts([
'post_type'        => 'meetings',
'post_status'      => 'any',
'ping_status'      => 'any',
'suppress_filters' => true,
'post_parent'      => null
]);

当我制作一个新的帖子类型的"会议"时,我可以确认它是用以下状态创建的:

{
"ID":100,
"post_author":"1",
"post_date":"2020-08-12 16:26:45",
"post_date_gmt":"2020-08-12 16:26:45",
"post_content":"",
"post_title":"Test",
"post_excerpt":"",
"post_status":"publish",
"comment_status":"closed",
"ping_status":"closed",
"post_password":"",
"post_name":"test-2",
"to_ping":"",
"pinged":"",
"post_modified":"2020-08-12 16:43:43",
"post_modified_gmt":"2020-08-12 16:43:43",
"post_content_filtered":"",
"post_parent":0,
"guid":"http:\/\/esh.localhost\/?post_type=meetings&p=100",
"menu_order":0,
"post_type":"meetings",
"post_mime_type":"",
"comment_count":"0",
"filter":"raw"
}

以下是注册此自定义post_type:的代码

register_post_type( 'meetings', [
'show_ui' => true,
'labels'  => array(
'name' => 'Meetings',
'singular_name' => 'Meeting',
'add_new' => 'New Meeting',
'all_items' => 'All Meetings',
'add_new_item' => 'New Meeting',
'edit_item' => 'Edit Meeting',
'new_item' => 'New Meeting',
'view_item' => 'View Meeting',
'search_items' => 'Search Meetings',
'not_found' =>  'No meetings found',
'not_found_in_trash' => 'No Meetings found in trash',
'parent_item_colon' => 'Parent Meeting:',
// 'parent' => 'eSupport',
// 'menu_name' => 'eSupport'
),
'show_in_graphql' => true,
'hierarchical' => true,
'graphql_single_name' => 'meeting',
'graphql_plural_name' => 'meetings',
'show_in_menu' => 'esh-sections'
]);

因此,有了这些帖子的详细信息,有人能理解为什么get_posts可能一直返回一个空集吗?即使对于常规帖子('post_type'=>'all'(,它仍然是空的。

如果有帮助的话,这里是确切的代码和输出:

$posts = get_posts([
'post_type'        => 'meetings',
'post_status'      => 'any',
'posts_per_page'   => -1
]);
dlog("Post type exists?", post_type_exists( 'meetings' ) );
dlog("posts", json_encode($posts));

输出:

[Wed Aug 12 13:09:49.732013 2020] [php7:notice] [pid 4371] [client ::1:60535] nPost type exists?, referer: http://localhost:3000/dashboard
[Wed Aug 12 13:09:49.732072 2020] [php7:notice] [pid 4371] [client ::1:60535] n1, referer: http://localhost:3000/dashboard
[Wed Aug 12 13:09:49.732105 2020] [php7:notice] [pid 4371] [client ::1:60535] nposts, referer: http://localhost:3000/dashboard
[Wed Aug 12 13:09:49.732119 2020] [php7:notice] [pid 4371] [client ::1:60535] n[], referer: http://localhost:3000/dashboard

您应该使用post_status => any

$posts = get_posts([
'post_type' => 'meetings',
'post_status' => 'any',
'posts_per_page' => -1
]);
var_dump($posts);

啊,我很抱歉,我有一个过滤返回的操作:

add_action('pre_get_posts', 'ESHMeetings::get_meetings_by_owner' );

禁用这些可以让我看到帖子。抱歉造成混乱!

相关内容

  • 没有找到相关文章

最新更新