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
  • You request via post_type only the type post. 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:21
  • Thanks for your reply and yes, 'dead' is only a custom status not a custom post type. So, I only want posts. As mentioned, if I set the post_status to only 'dead' I do indeed get just the dead status posts so it's not that the post_status parameter cannot recognize my 'dead' status at all. Only when I try to make it part of an array of post statuses. – Mojamba Commented Oct 19, 2021 at 8:30
  • 1 Try runing the code without tax_query, maybe you dont have any posts with dead status and with that taxonomy. Also the new wp_query( $args ) should be new WP_Query( $args ) (Even though classes are case insensitive, this is still good practice). – Buttered_Toast Commented Oct 19, 2021 at 13:38
  • 1 Possibly a long-shot, but it might help us to help you if you can also post the code you use to register your custom status...seems like the default parameters would be fine, but you never know, we might spot a clue there. – Trisha Commented Oct 19, 2021 at 19:58
  • 1 There is likely a plugin or something in your theme causing the problem. Are you filtering these posts in another context that isn't properly scoped? Any pre_get_posts hooks, for example? Recommend switching themes and deactivating plugins temporarily to debug. – jdm2112 Commented Oct 20, 2021 at 14:00
 |  Show 9 more comments

1 Answer 1

Reset to default 1

I 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