Last active
September 21, 2015 11:10
-
-
Save jeffikus/176061e6493ee7db49da to your computer and use it in GitHub Desktop.
Get all categories from an array of posts
This file contains 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
<?php | |
function get_posts_categories_optimized( $number_posts = 1000, $category_terms = array() ) { | |
$post_id_array = get_posts(array( | |
'numberposts' => $number_posts, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'category', | |
'field' => 'id', | |
'terms' => $category_terms | |
), | |
), | |
'fields' => 'ids', // Only get post IDs | |
)); | |
$post_cats = wp_get_object_terms( $post_id_array, 'category' ); | |
return $post_cats; | |
} // End get_posts_categories_optimized() | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment