Taxnomy Q: All categories (posts>0) with posts in term X of another taxonomy

View: New views
4 Messages — Rating Filter:   Alert me  

Taxnomy Q: All categories (posts>0) with posts in term X of another taxonomy

by Dion Hulse (dd32) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All,
I'm looking for a function which I'm pretty sure isnt offered by  
WordPress's Taxonomy system, Yet it seems to be something a few people  
might need.. So i'm putting a call out to see if anyone has any ideas?  
(ie. pre-written code they'd like to share..)

What i'm basically wanting, Is to find out the list of categories with  
posts > 0 which exist in term X of another taxonomy..

An example, Would be, Find all categories with more than 0 posts which are  
contained within the tag 'SomeTag'

I'm certain theres a easy SQL for this, But I dont really have the time to  
dive into it right now.. And caching would also be an issue (Given this is  
a list that'll be displayed on every page load, I'll probably just  
transient the results..)

Any thoughts or hints from anyone to save me some time?

Cheers
Dion Hulse

e: contact@...
w: http://dd32.id.au
WordPressQI: http://wordpressqi.com/
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Taxnomy Q: All categories (posts>0) with posts in term X of another taxonomy

by Austin Matzko :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 22, 2009 at 5:23 AM, Dion Hulse (dd32) <wordpress@...> wrote:

> I'm looking for a function which I'm pretty sure isnt offered by WordPress's
> Taxonomy system, Yet it seems to be something a few people might need.. So
> i'm putting a call out to see if anyone has any ideas? (ie. pre-written code
> they'd like to share..)
>
> What i'm basically wanting, Is to find out the list of categories with posts
>> 0 which exist in term X of another taxonomy..
>
> An example, Would be, Find all categories with more than 0 posts which are
> contained within the tag 'SomeTag'

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'));
}
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Taxnomy Q: All categories (posts>0) with posts in term X of another taxonomy

by Dion Hulse (dd32) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 22 Oct 2009 21:43:57 +1100, Austin Matzko <if.website@...>  
wrote:

> On Thu, Oct 22, 2009 at 5:23 AM, Dion Hulse (dd32)  
> <wordpress@...> wrote:
>> I'm looking for a function which I'm pretty sure isnt offered by  
>> WordPress's
>> Taxonomy system, Yet it seems to be something a few people might need..  
>> So
>> i'm putting a call out to see if anyone has any ideas? (ie. pre-written  
>> code
>> they'd like to share..)
>>
>> What i'm basically wanting, Is to find out the list of categories with  
>> posts
>>> 0 which exist in term X of another taxonomy..
>>
>> An example, Would be, Find all categories with more than 0 posts which  
>> are
>> contained within the tag 'SomeTag'
>
> 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'));
> }

Hmm... That may indeed work.. Told you i needed a push in the right  
directiion.

I'll give it a try tomorrow! Thanks Austin
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Taxnomy Q: All categories (posts>0) with posts in term X of another taxonomy

by Dion Hulse (dd32) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 22 Oct 2009 21:43:57 +1100, Austin Matzko <if.website@...>  
wrote:
> 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.
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers