Skip to content

Instantly share code, notes, and snippets.

@developeryamhi
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developeryamhi/c4d7134210a132781074 to your computer and use it in GitHub Desktop.
Save developeryamhi/c4d7134210a132781074 to your computer and use it in GitHub Desktop.
Get the Connected Taxonomies in WordPress
// Get the Connected Taxonomies via Posts
function get_the_connected_taxonomies($tax_name, $rel_tax_type = 'post_tag', $tax_type = 'category', $post_type = 'post', $from_cache = true, $search_key = 'slug') {
// Transient Key
$transient_key = $post_type . '-' . $rel_tax_type . '-' . $tax_type . '-' . $tax_name . '-cache';
// Check
if($from_cache && get_transient($transient_key)) {
// Return
return get_transient($transient_key);
}
// Tax Lists
$tax_lists = array();
// Query the Posts
$query = new WP_Query(array(
'post_type' => $post_type,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $tax_type,
'field' => $search_key,
'terms' => $tax_name,
)
)
));
// Loop Posts in Taxonomy
foreach($query->posts as $post) {
// Get the Taxonomies
$the_taxs = get_the_terms($post->ID, $rel_tax_type);
// Check
if($the_taxs) {
// Loop Each
foreach($the_taxs as $the_tax) {
// Check
if(!isset($tax_lists[$the_tax->term_id])) {
// Store Taxonomy
$tax_lists[$the_tax->term_id] = $the_tax;
}
}
}
}
// Check
if(sizeof($tax_lists) > 0) {
// Set Transient for 30 minutes
set_transient( $transient_key, $tax_lists, 60 * 30 );
}
// Return
return $tax_lists;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment