Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.