[wp-hackers] Taxnomy Q: All categories (posts>0) with posts in term X of another taxonomy

Dion Hulse (dd32) wordpress at dd32.id.au
Sun Nov 1 08:36:30 UTC 2009

On Thu, 22 Oct 2009 21:43:57 +1100, Austin Matzko <if.website at gmail.com>  
> Unless I misunderstand what you want, wouldn't the following work?
> $term = get_term_by('name', 'SomeTag', 'post_tag');
> if ( ! empty( $term->term_id ) ) {
> 	$cat_ids = (array) wp_get_object_terms(get_objects_in_term(
> array($term->term_id), array('post_tag')), array('category'),
> array('fields' => 'ids'));
> }

Finally got back to implementing this part of the project, That pretty  
much worked, With one gotcha.. get_objects_in_term() doesnt care what kind  
of object its returning, Could be an attachment, a post, a DRAFT post, or  
even a Trashed post..

I ended up splitting it up, and putting a query in between to filter it to  
valid post_ids.. Seems to work reasonably well

$term = get_term_by('name', 'SomeTag', 'post_tag');
if ( ! empty( $term->term_id ) ) {
	$ids = get_objects_in_term(array($term->term_id), array('post_tag'));
	global $wpdb;
	$ids = array_map('intval', $ids);
	$ids = implode(',', $ids);
	$ids = (array)$wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE ID  
IN($ids) AND post_type='post' AND post_status='publish'");
  	$cat_ids = (array) wp_get_object_terms($ids, array('category'),  
array('fields' => 'ids'));

So thanks Austin once again, Ended up saving me a heap of time.

More information about the wp-hackers mailing list