> For the complete documentation index, see [llms.txt](https://learnphp.gitbook.io/learnphp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learnphp.gitbook.io/learnphp/wordpress-advand/get-all-posts-id-from-a-category-emedic-ok.md).

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

Đọ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><br>

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

<figure><img src="/files/ibHkbEtMZBOiM3Lpz9XU" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/PGY3lVxaeWqsseRgWbXM" alt=""><figcaption></figcaption></figure>

```php
function wpse71471_get_post_ids($cat, $taxonomy = 'category')
{
  return get_posts(array(
    'numberposts'   => -1, // get all posts.
    'tax_query'     => array(
      array(
        'taxonomy'  => $taxonomy,
        'field'     => 'id',
        'terms'     => is_array($cat) ? $cat : array($cat),
      ),
    ),
    'fields'        => 'ids', // only get post IDs.
  ));
}
```

```php
$test = wpse71471_get_post_ids([12]);
echo '<pre>';
var_export($test);
echo '</pre>';
```

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

<figure><img src="/files/0tTI0zhYA6I2fYt6GErq" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/2XlHoXUxoVtLGhesPvAn" alt=""><figcaption></figcaption></figure>

```php
$test = wpse71471_get_post_ids([12,1]);
echo '<pre>';
var_export($test);
echo '</pre>';
```

## [get all posts ID from a category](https://wordpress.stackexchange.com/questions/71471/get-all-posts-id-from-a-category)

[Ask Question](https://wordpress.stackexchange.com/questions/ask)Asked 10 years, 8 months agoModified [7 years, 9 months ago](https://wordpress.stackexchange.com/questions/71471/get-all-posts-id-from-a-category?lastactivity)Viewed 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:

```php
$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

* [posts](https://wordpress.stackexchange.com/questions/tagged/posts)
* [get-posts](https://wordpress.stackexchange.com/questions/tagged/get-posts)
* [id](https://wordpress.stackexchange.com/questions/tagged/id)

[Share](https://wordpress.stackexchange.com/q/71471)[Improve this question](https://wordpress.stackexchange.com/posts/71471/edit)Followasked Nov 4, 2012 at 1:11[![user983248's user avatar](https://www.gravatar.com/avatar/7c791ae2031d5d15bb3026d25a3c3eb4?s=64\&d=identicon\&r=PG)](https://wordpress.stackexchange.com/users/15704/user983248)[user983248](https://wordpress.stackexchange.com/users/15704/user983248)1,35033 gold badges2020 silver badges3030 bronze badges[Add a comment](https://wordpress.stackexchange.com/questions/71471/get-all-posts-id-from-a-category#)

### 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:

```php
<?php
/**
 * Retrieve list of latest posts or posts matching criteria.
 *
 * The defaults are as follows:
 *     'numberposts' - Default is 5. Total number of posts to retrieve.
 *     'offset' - Default is 0. See {@link WP_Query::query()} for more.
 *     'category' - What category to pull the posts from.
 *     'orderby' - Default is 'post_date'. How to order the posts.
 *     'order' - Default is 'DESC'. The order to retrieve the posts.
 *     'include' - See {@link WP_Query::query()} for more.
 *     'exclude' - See {@link WP_Query::query()} for more.
 *     'meta_key' - See {@link WP_Query::query()} for more.
 *     'meta_value' - See {@link WP_Query::query()} for more.
 *     'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
 *     'post_parent' - The parent of the post or post type.
 *     'post_status' - Default is 'publish'. Post status to retrieve.
 *
 * @since 1.2.0
 * @uses $wpdb
 * @uses WP_Query::query() See for more default arguments and information.
 * @link http://codex.wordpress.org/Template_Tags/get_posts
 *
 * @param array $args Optional. Overrides defaults.
 * @return array List of posts.
 */
function get_posts($args = null) {
    $defaults = array(
        'numberposts' => 5, 'offset' => 0,
        'category' => 0, 'orderby' => 'post_date',
        'order' => 'DESC', 'include' => array(),
        'exclude' => array(), 'meta_key' => '',
        'meta_value' =>'', 'post_type' => 'post',
        'suppress_filters' => true
    );

    $r = wp_parse_args( $args, $defaults );
    if ( empty( $r['post_status'] ) )
        $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
    if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
        $r['posts_per_page'] = $r['numberposts'];
    if ( ! empty($r['category']) )
        $r['cat'] = $r['category'];
    if ( ! empty($r['include']) ) {
        $incposts = wp_parse_id_list( $r['include'] );
        $r['posts_per_page'] = count($incposts);  // only the number of posts included
        $r['post__in'] = $incposts;
    } elseif ( ! empty($r['exclude']) )
        $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );

    $r['ignore_sticky_posts'] = true;
    $r['no_found_rows'] = true;

    $get_posts = new WP_Query;
    return $get_posts->query($r);

}
```

Which, means, of course, that you can use any of the same arguments that `WP_Query` accepts. This includes parameters relating to [fields](http://codex.wordpress.org/Class_Reference/WP_Query#Post_Field_Parameters).

To get an array of just IDs, you'd need to do something like this:

```php
<?php
$post_ids = get_posts(array(
    'numberposts'   => -1, // get all posts.
    'tax_query'     => array(
        array(
            'taxonomy'  => 'category',
            'field'     => 'id',
            'terms'     => 5,
        ),
    ),
    'fields'        => 'ids', // Only get post IDs
));
```

Or you can wrap it up in a function for more flexibility.

```php
<?php
function wpse71471_get_post_ids($cat, $taxonomy='category')
{
    return get_posts(array(
        'numberposts'   => -1, // get all posts.
        'tax_query'     => array(
            array(
                'taxonomy'  => $taxonomy,
                'field'     => 'id',
                'terms'     => is_array($cat) ? $cat : array($cat),
            ),
        ),
        'fields'        => 'ids', // only get post IDs.
    ));
}
```

[Share](https://wordpress.stackexchange.com/a/71473)[Improve this answer](https://wordpress.stackexchange.com/posts/71473/edit)Followanswered Nov 4, 2012 at 1:30[![chrisguitarguy's user avatar](https://www.gravatar.com/avatar/9eea02bbe5255f8cc7d974977a3236c4?s=64\&d=identicon\&r=PG)](https://wordpress.stackexchange.com/users/6035/chrisguitarguy)[chrisguitarguy](https://wordpress.stackexchange.com/users/6035/chrisguitarguy)21.3k44 gold badges6060 silver badges9999 bronze badges[Add a comment](https://wordpress.stackexchange.com/questions/71471/get-all-posts-id-from-a-category#)0

Please refer to the following post: [Get post ids from WP\_Query?](https://wordpress.stackexchange.com/questions/166029/get-post-ids-from-wp-query)

You should be using: [wp\_list\_pluck](https://codex.wordpress.org/Function_Reference/wp_list_pluck)

**Example:**

```php
$ids = get_posts( array(
    'post_type' => 'contact',
    'pages_per_post' => -1,
) );

var_dump(wp_list_pluck( $ids, 'ID' ));
```
