相同的基本 WP_Query 适用于页面但不适用于 post 或自定义 post 类型
Same basic WP_Query works on page but not post or custom post type
$applicationQuery = new WP_Query(array( 'post_type' => 'page', 'post_status' => 'any' ));
if($applicationQuery->have_posts()){
$header_text = "Got post (GOOD)";
}
else{
$header_text = "No post (BAD)";
}
同样的查询,如果我对 post_type
使用 page
,查询中会有结果,但是如果我将 post_type
更改为 post
或我自己的 custom_post_type
没有结果。为什么?我该如何解决这个问题并查询我的自定义 post 类型?
我通过将 'post_status' => 'published'
添加到查询数组来解决问题。
编辑: 根本原因是因为自定义帖子是以编程方式创建的(不是通过管理仪表板),并且这些帖子的 post_status
被设置为 published
.
Wordpress 不识别 published
,默认情况下它只识别 publish
。查询中的 any
值实际上并不查找 "any" post_status,而是 "any recognized" post_status.
试试这个代码。
$applicationQuery = query_posts( array( 'post_type' => array('page') ) );
while (have_posts()) : the_post();
//echo the_title();
endwhile;
$applicationQuery = new WP_Query(array( 'post_type' => 'page', 'post_status' => 'any' ));
if($applicationQuery->have_posts()){
$header_text = "Got post (GOOD)";
}
else{
$header_text = "No post (BAD)";
}
同样的查询,如果我对 post_type
使用 page
,查询中会有结果,但是如果我将 post_type
更改为 post
或我自己的 custom_post_type
没有结果。为什么?我该如何解决这个问题并查询我的自定义 post 类型?
我通过将 'post_status' => 'published'
添加到查询数组来解决问题。
编辑: 根本原因是因为自定义帖子是以编程方式创建的(不是通过管理仪表板),并且这些帖子的 post_status
被设置为 published
.
Wordpress 不识别 published
,默认情况下它只识别 publish
。查询中的 any
值实际上并不查找 "any" post_status,而是 "any recognized" post_status.
试试这个代码。
$applicationQuery = query_posts( array( 'post_type' => array('page') ) );
while (have_posts()) : the_post();
//echo the_title();
endwhile;