Loop through posts of only 2 statuses

The question:

Setting up a visibility condition for a widget – i need it to apply to the posts in a certain term but only those of “pending” and “publish” status. this essentially hides the “add new post” button if they already have a post in that term that is pending or published. i just cant define the 2 statuses properly. i can define just 1 and it works perfect.

$type = 'my_cpt';
$tax = 'my_tax';
$term_name = 'my_term';
$term_id = 133;

    $args = array(
        'post_type'             => $type,
        'author'                => get_current_user_id(),
            'post_status' => array( 'pending', 'publish' ),
        'tax_query' => array(
            array(
            'taxonomy'      => $tax,
            'field'         => $term_name,
            'terms'         => $term_id,
            'include_children'  => false
            )
        )
    );
        
        $jobs = get_posts( $args );
        
        if(count($jobs) >= 1){?>
        
        <center>
            <?php echo ("You currently have ".count($jobs)." active Final Submission. Please delete it to add another one.");
            return true;
        }?>
        </center>

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Have you tried with “post_status” instead of “status” as documentation says: https://developer.wordpress.org/reference/classes/wp_query/#status-parameters ?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment