4

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="<?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!

3

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 );
  • Pieter, this seems like having better flexibility. Should i put this entire code in the page or functions.php? – Jornes Jun 6 '16 at 15:36
  • you should use it to extend your existing code – Pieter Goosen Jun 6 '16 at 15:40
4

From a quick look at the documentation for get_posts(), it doesn't look like you can exclude a post based on its category.

EDIT: Actually, thanks to AntonChanning, turns out you can - just add &cat=-1 to your argument string, where 1 is the ID of the category you wish to exclude:

$postslist = get_posts('numberposts=1&order=DESC&orderby=date&cat=-1');

You can also add multiple categories to this if you need to, eg. cat=-1,-2,-3


The preferred 'Wordpress way' to do this is using WP_Query.

If you want to do it this way, instead of your call to get_posts(), you'll need to create a new instance of WP_Query and pass your arguments through in an array. From the above link under the Category Parameters heading:

category__not_in (array) – use category id

That's the argument you'll want to use to exclude a category:

$postslist = new WP_Query(array(
  "post_type" => "post",
  "posts_per_page" => 1,
  "order" => "DESC"
  "orderby" => "date",
  "category__not_in" => 1,
));

You'll notice I also added the 'post' post type to mimic the behaviour you would have had with get_posts(), and the posts_per_page parameter is the way to specify the number of posts returned with WP_Query.

One other change you'll need to make if you do go down this WP_Query route is to use a while loop instead of the foreach you currently have. There's clear usage examples under the Usage heading in the docs so I'll avoid copying and pasting them out.

Good luck!

  • 4
    Any argument that is valid for WP_Query is valid for get_posts. get_posts is just a wrapper for WP_Query. All arguments (except a couple like numberposts and include and exclude) are passed unchanged/unmodified to WP_Query as is ;-) – Pieter Goosen Jun 6 '16 at 8:26
  • @PieterGoosen That makes two things I learnt while responding to this! Thanks Pieter. – Tim Malone Jun 6 '16 at 8:32
  • 1
    To exclude multiple categories, just deliminate with a comma: cat=-1,-2,-3 – AntonChanning Jun 6 '16 at 8:37
  • Thanks again for your help with this @AntonChanning. I've just added that to the post too. – Tim Malone Jun 6 '16 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.