Problem: I need to get an array of ID’s of posts from a given category If the category have any posts. This is to be used on a plugin options page.
So far I have:
$posts = get_posts(array('numberposts' => 10000, 'category' => 5));
But I’m struggling with How do I gen an array containing just the ID of each post on that category.
Any Ideas ?
Thanks
The thing to remember about
get_posts
is that is uses aWP_Query
object internally.get_posts
source:Which, means, of course, that you can use any of the same arguments that
WP_Query
accepts. This includes parameters relating to fields.To get an array of just IDs, you’d need to do something like this:
Or you can wrap it up in a function for more flexibility.
Please refer to the following post: Get post ids from WP_Query?
You should be using: wp_list_pluck
Example: