Home > database >  Wordpress fetch blog posts excluding a category ID
Wordpress fetch blog posts excluding a category ID

Time:04-22

I am trying to show the latest blog posts on my wordpress website. I am working with this code, which shows the latest 3 blog posts with any category.

<?php 
                
$the_query = new WP_Query( 'posts_per_page=3');     
while ($the_query -> have_posts()) : $the_query -> the_post(); 

?>



<!-- main content here -->  
<div > 
<?php echo wp_trim_words( get_the_excerpt(), 20, ' ...' ); ?>
</div> 
        
                
<?php 

endwhile;
wp_reset_postdata();    
                        
?>

Now I'd like to exclude a post category from this script (all posts with the category ID 17 should be excluded. I've tried the following scripts, but either way it won't show any posts (all the code stays the same, except the first few lines I changed and tried)

This one did not show anything

$the_query = new WP_Query( 'posts_per_page=3', 'cat=-17'); 

Tried with array:

$the_query = new WP_Query($argsQuery); 

$argsQuery = array(
    'posts_per_page' => 3,
    'cat' => '-17',
);

Am I missing something here?

CodePudding user response:

Try this:

$the_query = new WP_Query( array( 'cat' => '-17', 'posts_per_page' => '3' ) );
  • Related