27

Is there a way I can retrieve an array of post ids queried from the following:

$latest = new WP_Query( array (
    'orderby'               => 'rand',
    'posts_per_page'        => 3
));

if ( $latest -> have_posts() ) : while ( $latest -> have_posts() ) : $latest -> the_post();

    get_template_part( 'templates/content', 'post' );

endwhile; endif; wp_reset_postdata();

Follow Up:

I used wp_list_pluck to retrieve an array of post ids:

$post_ids = wp_list_pluck( $latest->posts, 'ID' );

Then converted the array into a string using the implode function:

$post_ids_string = implode( ',', $post_ids );

Sorry for the ambiguous question.

  • rick, your question is ambiguous. Please be very clear what you want before posting a question. This will save everyone from answering irrelevant stuff. You actually need the post ID's returned in a string, not an array – Pieter Goosen Oct 21 '14 at 16:01
  • 1
    Your wp_reset_postdata should be inside not outside the if statement, otherwise you might reset post data when it hasn't been changed – Tom J Nowell Oct 21 '14 at 16:19
  • 1
    If you're only wanting the IDs, you should seriously consider s_ha_dum's answer. That will return the IDs without also retrieving lots of other data from the database that you then throw away. – Chris Rae Feb 27 '15 at 21:51
43

Try

$post_ids = wp_list_pluck( $latest->posts, 'ID' );

Read wp_list_pluck

  • 5
    This can be useful if you also need the whole data for each post, not just the posts ids. Otherwise, I'd go with @s-ha-dum's solution. – Marian Mar 31 '17 at 8:09
74

Use the fields argument in your query.

fields (string) - Which fields to return. All fields are returned by
default. There are two other options: - 'ids' - Return an array of post IDs. - 'id=>parent' - Return an associative array [ parent => ID, … ].

http://codex.wordpress.org/Class_Reference/WP_Query#Return_Fields_Parameter

$latest = new WP_Query( array (
    'orderby'               => 'rand',
    'posts_per_page'        => 3,
    'fields' => 'ids'
));
var_dump($latest->posts);
  • 12
    This should be the accepted answer as it only queries the ID's making it a lot faster than quering everyhing and than 'plucking' (loop and re-storing) it in a new array. – Barry Kooij Feb 5 '16 at 16:31
  • Perhaps you need to get IDs AFTER normal wp_query has already been executed, for example when you merge two queries afterwards and need ID's to exclude results from previous query. – trainoasis Aug 5 at 9:09
2

Using the solution from @s-ha-dum is economical if you only need to get the id's, and you don't have previous query object set.

Here is why:

switch ( $q['fields'] ) {
    case 'ids':
        $fields = "$wpdb->posts.ID";
        break;
    case 'id=>parent':
        $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
        break;
    default:
        $fields = "$wpdb->posts.*";

Because in the case you only specify 'fields' => 'ids' nothing more you will get in return than the ID's.

If you would go with 'fields' => 'id=>parent' (Looks really funny) you will get also the parent ID's.

Any other way using 'fields' argument will not have any impact as of WordPress v4.7.

But in case you have the query as in the example wp_list_pluck will do the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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