Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

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 AntonChanningAntonChanning, 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!

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!

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!

Added additional clarification thanks to @AntonChanning & @PieterGoosen in the comments
Source Link
Tim Malone
  • 4.8k
  • 5
  • 28
  • 41

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 pointing it out, 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


Another wayThe preferred 'Wordpress way' to do this is using WP_Query, which will give you a lot of other options for modifying your query (see that link for details)using WP_Query.

InsteadIf 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()get_posts(), and also the posts_per_pageposts_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 (which I strongly advise, it'll give you much more options for future queries!) is to use a while loop instead of the foreach you currently have. There's clear usage examplesclear usage examples under the Usage heading in the docs so I'll avoid copying and pasting them out.

Good luck!

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 pointing it out, 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');

Another way to do this is using WP_Query, which will give you a lot of other options for modifying your query (see that link for details).

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:

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 also 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 (which I strongly advise, it'll give you much more options for future queries!) is to use a while loop instead of the foreach you currently have. There's clear usage examples in the docs so I'll avoid copying and pasting them out.

Good luck!

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!

Added additional clarification
Source Link
Tim Malone
  • 4.8k
  • 5
  • 28
  • 41

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 pointing it out, 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');

Another way to do this is using WP_Query, which will give you a lot of other options for modifying your query (see that link for details).

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:

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 also 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 (which I strongly advise, it'll give you much more options for future queries!) is to use a while loop instead of the foreach you currently have. There's clear usage examples in the docs so I'll avoid copying and pasting them out.

Good luck!

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 pointing it out, 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');

Another way to do this is using WP_Query, which will give you a lot of other options for modifying your query (see that link for details).

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:

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,
));

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 pointing it out, 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');

Another way to do this is using WP_Query, which will give you a lot of other options for modifying your query (see that link for details).

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:

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 also 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 (which I strongly advise, it'll give you much more options for future queries!) is to use a while loop instead of the foreach you currently have. There's clear usage examples in the docs so I'll avoid copying and pasting them out.

Good luck!

Add code samples
Source Link
Tim Malone
  • 4.8k
  • 5
  • 28
  • 41
Loading
Source Link
Tim Malone
  • 4.8k
  • 5
  • 28
  • 41
Loading