Show recent published posts

I want to list recent published posts of my wp blog and exclude certain posts of some categories.
The following code works fine, 10 recent posts are listed and posts in the listed categories are ignored. However, draft posts are listed as well.

$args = array( 'numberposts' => '10', 'tax_query' =>
 array(
     'post_type' => 'post',
     'post_status' => array( 'publish' ),
     'tax_query' => array(
         'relation' => 'AND',
         array(
             'taxonomy' => 'category',
             'field' => 'id',
             'terms' => array( 10, 11, 57 ),
             'operator' => 'NOT IN',
         ),
     ),
 )
);



$recent_posts = wp_get_recent_posts(  $args );
foreach( $recent_posts as $recent ){
    echo '<li><a href="' . get_permalink($recent["ID"]) . '">'.   $recent["post_title"].'</a> </li> ';
    }
?>

The line with 'post_status' => array( 'publish' ), or 'post_status' => 'publish', does not work. Any clue why?

1 Answer
1

The arguments you are using are wrong. They should be:

$args = array(
            'numberposts' => '10',
            'post_type'   => 'post',
            'post_status' =>'publish',
            'tax_query'   => array(
                'taxonomy' => 'category',
                'field' => 'id',
                'terms' => array( 10, 11, 57 ),
                'operator' => 'NOT IN',
            )
 );

Or shorter:

$args = array(
            'numberposts'        => '10',
            'post_type'          => 'post',
            'post_status'        => 'publish',
            'category__not_in'   => array( 10, 11, 57 )
 );

Leave a Comment