I am creating a website. There is a column for latest updates.
This is the code that i have now. I don’t really good in php coding. Would like to get guidance from someone.

<?php
 $postslist = get_posts('numberposts=1&order=DESC&orderby=date');
 foreach ($postslist as $post) :
    setup_postdata($post);
 ?>
<?php the_excerpt(); ?>
<p class="date"><?php echo get_the_date(); ?></p>
<span class="button">
<a href="https://wordpress.stackexchange.com/questions/228889/<?php the_permalink(); ?>">Read More</a>

But, i would like to exclude some posts from certain categories. Is there any solution to make it happen? Thank you!

2 Answers
2

In addition to the answer by Tim, one can always use a proper tax_query. All the build in tag and category parameters gets converted to a proper tax_query before being passed to the WP_Tax_Query class to build the JOIN clause for the SQL query.

I use a tax_query in almost all applications as it gives one a lot of flexibilty, specially when it comes to child terms, exclusions of multiple terms and even multiple taxonomies. Maybe the only downfall is that you cannot use the string syntax here as in the OP, bacause a tax_query is an array, one should use the array syntax for the query args

In short, to exclude categories, you can try the following

$args = [
    // Your other args
    'tax_query' => [
        [
            'taxonomy'         => 'category',
            'field'            => 'term_id',
            'terms'            => [1,2,3], // Array of term ids to exclude
            'operator'         => 'NOT IN', // Exclude
        'exclude_children' => false // Do not exclude the child terms from the terms defined to exclude
        ]
    ]
];
$postslist = get_posts( $args );

Leave a Reply

Your email address will not be published. Required fields are marked *