😄get all posts ID from a category Emedic (ok)

https://wordpress.stackexchange.com/questions/71471/get-all-posts-id-from-a-category

Đọc thêm: https://wordpress-lionel.gitbook.io/wordpress/kiem-tra-bai-viet-co-thuoc-chuyen-muc-check-post-is-in-the-category-dakhoathienhoa.com.vn-ok#vi-du-2-khong-hieu-sao-thuc-hien-tren-phongkhamdakhoathienhoa.com.vn-khong-chinh-xac

Ví dụ 1: Lấy 1 chuyên mục

Ví dụ 2: Lấy nhiều chuyên mục

Ask QuestionAsked 10 years, 8 months agoModified 7 years, 9 months agoViewed 44k times7

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:

But I'm struggling with How do I gen an array containing just the ID of each post on that category.

Any Ideas ? Thanks

ShareImprove this questionFollowasked Nov 4, 2012 at 1:11user983248's user avataruser9832481,35033 gold badges2020 silver badges3030 bronze badgesAdd a comment

2 Answers

Sorted by: Highest score (default) Date modified (newest first) Date created (oldest first) 16

The thing to remember about get_posts is that is uses a WP_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.

ShareImprove this answerFollowanswered Nov 4, 2012 at 1:30chrisguitarguy's user avatarchrisguitarguy21.3k44 gold badges6060 silver badges9999 bronze badgesAdd a comment0

Please refer to the following post: Get post ids from WP_Query?

You should be using: wp_list_pluck

Example:

Last updated

Was this helpful?