Skip to content

Instantly share code, notes, and snippets.

@szbl
Created July 17, 2012 01:12
Show Gist options
  • Save szbl/3126273 to your computer and use it in GitHub Desktop.
Save szbl/3126273 to your computer and use it in GitHub Desktop.
Pulling Shared Taxonomy Terms Based On Single Post Type
I have three custom post types that share a hierarchical custom taxonomy for Location. Root level terms are states, child terms are cities.
These post types are different types of "records" for this site's many "directories" if you will. I'd like to be able to share the taxonomy across all three but do the following with a core function/query/whatever:
Example page:
/post-type-1/states/
Description:
Shows a listing of all states (root-level terms) that have been assigned to a post with the type "post-type-1"
Example page:
/post-type-2/states/
Description:
Shows a listing of all states (root-level terms) that have been assigned to a post with the type "post-type-2"
...and so forth...
I know I can wp_list_categories() or something of the sort to get all terms that are non-empty, but I ALSO want to filter by the post type applied.
Any thoughts? We're short on time/budget so I'm about to just allow city/state to be stored in a meta field so I can do a get_posts()/WP_Query call with a meta_query array.
I don't often as for help / advice, so thanks in advance!
@zanematthew
Copy link

$posts_list = get_posts_in_tax('state');
$state_list = get_terms('states');
// compare those two
$final_posts = array();

foreach( $posts_list as $post ) {
if ( // some conditional )
$tmp = $something else;

$final_posts[] = $tmp;

}

// Template code or something
foreach( $final_posts as $post ){
print $post['my_state'];
}

@szbl
Copy link
Author

szbl commented Jul 17, 2012

Don't see get_posts_in_tax()......

@zanematthew
Copy link

Sorry, I was thinking of this: http://codex.wordpress.org/Function_Reference/get_posts (or you would use wp_query)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment