Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Created April 6, 2015 21:58
Show Gist options
  • Save hellofromtonya/866862794445bc4c87bf to your computer and use it in GitHub Desktop.
Save hellofromtonya/866862794445bc4c87bf to your computer and use it in GitHub Desktop.
Get all Post IDs for the current WP_Query
<?php
add_action( 'loop_end', 'tonya_get_list_of_post_ids_in_current_loop' );
/**
* Get a list of Post IDs for the current Loop.
*
* Hooks into 'loop_end' which occurs when WP_Query is done with the
* loop (see wp-includes/query.php).
*
* @since 1.0.0
*
* @param WP_Query obj $query WP_Query object instance
* @return void
*/
function tonya_get_list_of_post_ids_in_current_loop( $query ) {
$ids = array();
foreach ( $query->posts as $post ) {
$ids[] = $post->ID;
}
//* Now do something with it.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment