Skip to content

Instantly share code, notes, and snippets.

@jeffikus
Last active September 21, 2015 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffikus/176061e6493ee7db49da to your computer and use it in GitHub Desktop.
Save jeffikus/176061e6493ee7db49da to your computer and use it in GitHub Desktop.
Get all categories from an array of posts
<?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