0
我在自定义页面模板上使用get_posts以列出用户是其作者的特定类型的帖子(本例中为business
)。 这里是我想要的代码:Wordpress get_posts无法正常工作
$args = array(
'author' => get_current_user_id(),
'post_type' => 'business',
'posts_per_page' => -1,
'post_status' => array(
'publish',
'pending',
)
);
$listings = get_posts($args);
然而,这种由用户返回无论类型的所有职位。如果我删除了author
参数,我会得到所需类型的所有帖子,而无需考虑作者。 get_current_user_id()
确实会返回正确的用户标识,但我尝试在代码中添加标识以及获得相同的结果。
有没有人知道为什么会发生这种情况?
我使用WP 4.6.1
UPDATE: SQL日志显示了以下
如果我使用author
参数:
SELECT wp_posts.*
FROM wp_posts
WHERE 1=1
AND wp_posts.post_author IN (1)
AND wp_posts.post_type IN ('post', 'project', 'profile', 'campaign')
AND ((wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'pending'))
ORDER BY wp_posts.post_date DESC
如果我离开author
参数出来:
SELECT wp_posts.*
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = 'business'
AND ((wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'pending'))
ORDER BY wp_posts.post_date DESC
而且,这里是自定义文章类型声明:
register_post_type('business',
array(
'labels' => array(
'name' => __('Businesses'),
'singular_name' => __('Business'),
'add_new' => __('Add New Business'),
'add_new_item' => __('Add New Business'),
'edit_item' => __('Edit Business')
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-store',
'supports' => array('thumbnail', 'title', 'editor', 'author')
)
);
我在这里损失...
你的论点看起来是正确的。你可以显示'register_post_type'代码,所以我们可以看到你用来注册帖子的类型吗? –
另外,[查看SQL语句]可能很有用(http://wordpress.stackexchange.com/questions/110266/how-to-print-the-excuted-sql-right-after-its-execution)当你执行这个'get_posts'时运行。 –