Skip to content

Instantly share code, notes, and snippets.

@spacedmonkey
Created November 18, 2022 14:47
Show Gist options
  • Save spacedmonkey/d8d0a822d05d716fcfd1656caa909c97 to your computer and use it in GitHub Desktop.
Save spacedmonkey/d8d0a822d05d716fcfd1656caa909c97 to your computer and use it in GitHub Desktop.
$post_type_taxonomies = array();
$term_ids = array();
$prime_post_terms = array();
foreach ( $posts as $post ) {
if ( ! ( $post instanceof WP_Post ) ) {
continue;
}
if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) {
$post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type );
}
foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
$prime_post_terms[ $taxonomy ][] = $post->ID;
}
}
foreach( $prime_post_terms as $taxonomy => $post_ids ){
wp_cache_get_multiple( $post_ids, "{$taxonomy}_relationships" );
}
foreach ( $posts as $post ) {
foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
// Term cache should already be primed by `update_post_term_cache()`.
$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false !== $terms ) {
foreach ( $terms as $term ) {
if ( ! in_array( $term->term_id, $term_ids, true ) ) {
$term_ids[] = $term->term_id;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment