WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 11 down vote accepted

The thing to remember about get_posts is that is uses a WP_Query object internally. get_posts source:

<?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.

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

<?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
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|improve this answer
    
THANKS _ A LOT !!! – user983248 Nov 4 '12 at 1:43

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

You should be using: wp_list_pluck

Example:

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

var_dump(wp_list_pluck( $ids, 'ID' ));
share|improve this answer
    
This is still very very expensive. You are returning the complete post object of every post. if you have 1000+ posts, your query can time out. The easiest and best solution is to set the fields parameter to ids, this will only return ids. This saves a HUGE amount on db calls and time send to run the query. – Pieter Goosen Oct 27 '15 at 4:21
    
The answer from @s_ha_dum from the linked post is the correct way if only want ID's from the post. – Pieter Goosen Oct 27 '15 at 4:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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