Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Created October 18, 2011 04:29
Show Gist options
  • Save thenbrent/1294615 to your computer and use it in GitHub Desktop.
Save thenbrent/1294615 to your computer and use it in GitHub Desktop.
Unobtrusively get the IDs of ALL posts in a WordPress query, not just the posts on the current page
/**
* Unobtrusively gets the IDs of all posts in the current query, not just the posts shown on the current page.
*
* @author Brent Shepherd <[email protected]>
* @since 1.0
*/
function eg_get_all_posts_in_query() {
global $wp_query;
// Turn paging off & specify that we only want post IDs
$wp_query->set( 'nopaging', true );
$wp_query->set( 'fields', 'ids' );
$post_ids = $wp_query->get_posts();
// Reset the query
$wp_query->set( 'nopaging', false );
$wp_query->set( 'fields', '' );
$wp_query->get_posts();
return $post_ids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment