I addressed a similar problem not long ago – it’s all in the memory:
$post_ids = get_posts(
array(
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids', // Just grab IDs instead of pulling 1000's of objects into memory
)
);
update_object_term_cache( $post_ids, 'post' ); // Cache all the post terms in one query, memory should be ok
foreach ( $post_ids as $post_id ) {
if ( ! $tags = get_object_term_cache( $post_id, 'post_tag' ) ) {
$zerotags++;
} else {
$tag_count = count( $tags );
if ( $tag_count === 1 ) {
$onetag++;
} elseif ( $tag_count === 2 ) {
$twotags++;
} elseif ( $tag_count >= 6 ) {
$sixtags_plus++;
}
if ( $tag_count > 2 && $tag_count < 6 ) {
$morethantwo++;
}
}
}
Update: Switched get_the_tags
to get_object_term_cache
– otherwise we lose all our hard work! (the former hits get_post
, which will hit the db on every iteration and chuck the post object into memory – props @Pieter Goosen).
Update 2: The second argument for update_object_term_cache
should be the post type, not the taxonomy.