Created
October 18, 2011 04:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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