Skip to content

Instantly share code, notes, and snippets.

@luisfdeandrade
Created October 21, 2014 11:58
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 luisfdeandrade/57474a1f6525d6f22374 to your computer and use it in GitHub Desktop.
Save luisfdeandrade/57474a1f6525d6f22374 to your computer and use it in GitHub Desktop.
Wordpress - Taxonomy Intersection
<?php
/**
* Get all terms of $tax_to taxonomy that posts in $term_from of $tax_from have.
*
* @param string $tax_from taxonomy name
* @param string $term_from term slug
* @param string $tax_to taxonomy name
*
* @return array|WP_Error
*/
function get_intersected_terms( $tax_from, $term_from, $tax_to ) {
global $wpdb;
$term_from = get_term_by( 'slug', $term_from, $tax_from );
$query = "
SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = '{$tax_to}' AND term_taxonomy_id IN (
SELECT term_taxonomy_id FROM {$wpdb->term_relationships} WHERE object_id IN (
SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = {$term_from->term_taxonomy_id}
)
)
";
$term_ids = $wpdb->get_col( $query );
if( empty( $term_ids) )
return array();
return get_terms( $tax_to, array( 'include' => $term_ids ) );
}
// example
var_dump( get_intersected_terms( 'category', 'cat-a', 'post_tag' ) );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment