admin管理员组文章数量:1122832
I have a custom post status called 'dead.' I am trying to create a wp_query to show all posts for a custom taxonomy. If I specify 'any' for the post_status I do get published posts and my 'dead' post_status posts. Likewise, if I set the post_status parameter to only 'dead' I do get just those posts. But, if I try to set the post_status to show both published and dead post_status posts via an array I can only get the published ones.
$args = array(
'post_status' => array('publish', 'dead'),
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'post_title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'linktypes',
'field' => 'id',
'terms' => '15'
),
)
);
$my_query = new wp_query( $args );
Does WordPress not allow you to specify both standard and custom post_statuses in an array like this? Or am I just doing something wrong?
UPDATE: As recommended, I am adding the code I use to create the custom post_status (as well as the code to add it to the admin screen and quick edit screen just in case that might be relevant.
function custom_post_status() {
register_post_status( 'dead', array(
'label' => _x( 'Dead', 'post' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Dead <span class="count">(%s)</span>', 'Dead <span class="count">(%s)</span>' ),
) );
}
add_action( 'init', 'custom_post_status' );
function my_post_submitbox_misc_actions() {
global $post;
//only when editing a post
if( $post->post_type == 'post' ){
// custom post status: dead
$complete = '';
$label = '';
if( $post->post_status == 'dead' ){
$complete = ' selected=\"selected\"';
$label = '<span id=\"post-status-display\"> Dead</span>';
}
echo '<script>
jQuery(document).ready(function($){
$("select#post_status").append("<option value=\"publish\" selected=\"publish\">Publish</option>");
$("select#post_status").append("<option value=\"dead\" '.$complete.'>Dead</option>");
$(".misc-pub-section label").append("'.$label.'");
});
</script>';
}
}
add_action( 'post_submitbox_misc_actions', 'my_post_submitbox_misc_actions' );
function rudr_status_into_inline_edit() {
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"dead\">Dead</option>' );
});
</script>";
}
add_action('admin_footer-edit.php','rudr_status_into_inline_edit');
UPDATE #2: I added print_r($my_query->request);
to see the WP_Query request SQL and this is what I see.
using 'post_status' => array('dead')
:
SELECT [...] AND ((wp_posts.post_status = 'dead')) GROUP BY [...]
using 'post_status' => array('publish','dead')
:
SELECT [...] AND ((wp_posts.post_status = 'publish')) GROUP BY [...]
using 'post_status' => array('any')
:
SELECT [...] AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')) GROUP BY [...]
using 'post_status' => array('publish','future')
:
SELECT [...] AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future')) GROUP BY [...]
I have a custom post status called 'dead.' I am trying to create a wp_query to show all posts for a custom taxonomy. If I specify 'any' for the post_status I do get published posts and my 'dead' post_status posts. Likewise, if I set the post_status parameter to only 'dead' I do get just those posts. But, if I try to set the post_status to show both published and dead post_status posts via an array I can only get the published ones.
$args = array(
'post_status' => array('publish', 'dead'),
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'post_title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'linktypes',
'field' => 'id',
'terms' => '15'
),
)
);
$my_query = new wp_query( $args );
Does WordPress not allow you to specify both standard and custom post_statuses in an array like this? Or am I just doing something wrong?
UPDATE: As recommended, I am adding the code I use to create the custom post_status (as well as the code to add it to the admin screen and quick edit screen just in case that might be relevant.
function custom_post_status() {
register_post_status( 'dead', array(
'label' => _x( 'Dead', 'post' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Dead <span class="count">(%s)</span>', 'Dead <span class="count">(%s)</span>' ),
) );
}
add_action( 'init', 'custom_post_status' );
function my_post_submitbox_misc_actions() {
global $post;
//only when editing a post
if( $post->post_type == 'post' ){
// custom post status: dead
$complete = '';
$label = '';
if( $post->post_status == 'dead' ){
$complete = ' selected=\"selected\"';
$label = '<span id=\"post-status-display\"> Dead</span>';
}
echo '<script>
jQuery(document).ready(function($){
$("select#post_status").append("<option value=\"publish\" selected=\"publish\">Publish</option>");
$("select#post_status").append("<option value=\"dead\" '.$complete.'>Dead</option>");
$(".misc-pub-section label").append("'.$label.'");
});
</script>';
}
}
add_action( 'post_submitbox_misc_actions', 'my_post_submitbox_misc_actions' );
function rudr_status_into_inline_edit() {
echo "<script>
jQuery(document).ready( function() {
jQuery( 'select[name=\"_status\"]' ).append( '<option value=\"dead\">Dead</option>' );
});
</script>";
}
add_action('admin_footer-edit.php','rudr_status_into_inline_edit');
UPDATE #2: I added print_r($my_query->request);
to see the WP_Query request SQL and this is what I see.
using 'post_status' => array('dead')
:
SELECT [...] AND ((wp_posts.post_status = 'dead')) GROUP BY [...]
using 'post_status' => array('publish','dead')
:
SELECT [...] AND ((wp_posts.post_status = 'publish')) GROUP BY [...]
using 'post_status' => array('any')
:
SELECT [...] AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')) GROUP BY [...]
using 'post_status' => array('publish','future')
:
SELECT [...] AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future')) GROUP BY [...]
Share
Improve this question
edited Oct 20, 2021 at 6:03
Mojamba
asked Oct 19, 2021 at 8:04
MojambaMojamba
1146 bronze badges
14
|
Show 9 more comments
1 Answer
Reset to default 1I had the same issue and the reason was that I was running the query before registering the post status. So the post status was not registered and thus ignored in WP_Query
本文标签: wp querywpquery ignoring custom poststatus
版权声明:本文标题:wp query - wp_query ignoring custom post_status 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312350a1935069.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
post_type
only the typepost
. Have you only enhanced the custom post status? If not, you need to add also this post type, like'post_type' => array( 'post', 'dead' )
. – bueltge Commented Oct 19, 2021 at 8:21tax_query
, maybe you dont have any posts withdead
status and with that taxonomy. Also thenew wp_query( $args )
should benew WP_Query( $args )
(Even though classes are case insensitive, this is still good practice). – Buttered_Toast Commented Oct 19, 2021 at 13:38pre_get_posts
hooks, for example? Recommend switching themes and deactivating plugins temporarily to debug. – jdm2112 Commented Oct 20, 2021 at 14:00