Getting lowest/last-level terms in hierarchical taxonomy

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting lowest/last-level terms in hierarchical taxonomy

Nikola Nikolov
Here's an interesting one - how can I get all of the *last-level* terms in
a hierarchical taxonomy?

Take the following hierarchy for instance:

 - Food
 - - Vegetables
 - - - Cucumber
 - - - Carrot
 - - Bread
 - - Fruits
 - - - Citrus Fruits
 - - - - Orange
 - - - - Pomelo
 - Potato

( I know the potato doesn't make sense, but I just wanted to illustrate the
fact that some terms might be without children )

In this case, I'd want to grab the following terms: Cucumber, Carrot,
Bread, Orange, Pomelo, Potato.

I've been looking around I haven't found an easy solution so far.

I can think of two options - getting all terms and then recreating the tree
from them, or getting each level of terms and doing subsequent get_terms()
calls(which I don't like, since it's probably going to be very
inefficient).

My goal is to be able to present to the user a list of all last-level terms
in the taxonomy and then behind the scenes assign the post to all terms
from that tree(so, if you select Carrot, your post would be added to
Vegetables and Food).
I need to do that, since I have a custom table that stores post to post
relations and I need to be able to query for all items in a specific
category/list of categories with as little extra requests as possible(lots
of data is expected to be stored and performance is key).

Thanks,
Nikola
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Getting lowest/last-level terms in hierarchical taxonomy

Haluk Karamete
HI Nikola
what if you walk thru all terms in that tax and check if the current term
has children or not.

http://codex.wordpress.org/Function_Reference/get_term_children

Did I understand you correctly?
Here's an interesting one - how can I get all of the *last-level* terms in
a hierarchical taxonomy?

Take the following hierarchy for instance:

 - Food
 - - Vegetables
 - - - Cucumber
 - - - Carrot
 - - Bread
 - - Fruits
 - - - Citrus Fruits
 - - - - Orange
 - - - - Pomelo
 - Potato

( I know the potato doesn't make sense, but I just wanted to illustrate the
fact that some terms might be without children )

In this case, I'd want to grab the following terms: Cucumber, Carrot,
Bread, Orange, Pomelo, Potato.

I've been looking around I haven't found an easy solution so far.

I can think of two options - getting all terms and then recreating the tree
from them, or getting each level of terms and doing subsequent get_terms()
calls(which I don't like, since it's probably going to be very
inefficient).

My goal is to be able to present to the user a list of all last-level terms
in the taxonomy and then behind the scenes assign the post to all terms
from that tree(so, if you select Carrot, your post would be added to
Vegetables and Food).
I need to do that, since I have a custom table that stores post to post
relations and I need to be able to query for all items in a specific
category/list of categories with as little extra requests as possible(lots
of data is expected to be stored and performance is key).

Thanks,
Nikola
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Getting lowest/last-level terms in hierarchical taxonomy

Nikola Nikolov
Yeah, that's one my thoughts, but I'm afraid it will cause too many queries
perhaps? Although from looking at the code it seems like
get_term_children() is calling _get_term_hierarchy() every time, which on
the other hand is storing it's data in an option in the options table...
I guess that might work :)

Alternatively, I can store the last-level terms(once I fetch them) in an
option as well and flush that option when a term is added/updated/deleted
from that taxonomy.

Any other ideas?


On Mon, May 26, 2014 at 6:19 AM, Haluk Karamete <[hidden email]>wrote:

> HI Nikola
> what if you walk thru all terms in that tax and check if the current term
> has children or not.
>
> http://codex.wordpress.org/Function_Reference/get_term_children
>
> Did I understand you correctly?
> Here's an interesting one - how can I get all of the *last-level* terms in
> a hierarchical taxonomy?
>
> Take the following hierarchy for instance:
>
>  - Food
>  - - Vegetables
>  - - - Cucumber
>  - - - Carrot
>  - - Bread
>  - - Fruits
>  - - - Citrus Fruits
>  - - - - Orange
>  - - - - Pomelo
>  - Potato
>
> ( I know the potato doesn't make sense, but I just wanted to illustrate the
> fact that some terms might be without children )
>
> In this case, I'd want to grab the following terms: Cucumber, Carrot,
> Bread, Orange, Pomelo, Potato.
>
> I've been looking around I haven't found an easy solution so far.
>
> I can think of two options - getting all terms and then recreating the tree
> from them, or getting each level of terms and doing subsequent get_terms()
> calls(which I don't like, since it's probably going to be very
> inefficient).
>
> My goal is to be able to present to the user a list of all last-level terms
> in the taxonomy and then behind the scenes assign the post to all terms
> from that tree(so, if you select Carrot, your post would be added to
> Vegetables and Food).
> I need to do that, since I have a custom table that stores post to post
> relations and I need to be able to query for all items in a specific
> category/list of categories with as little extra requests as possible(lots
> of data is expected to be stored and performance is key).
>
> Thanks,
> Nikola
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Getting lowest/last-level terms in hierarchical taxonomy

Simon Vart
One idea could be to create a transient to store the results of your query
for one day (or more) on first parsing, then delete it each time a category
is create/deleted/updated


2014-05-26 10:33 GMT+02:00 Nikola Nikolov <[hidden email]>:

> Yeah, that's one my thoughts, but I'm afraid it will cause too many queries
> perhaps? Although from looking at the code it seems like
> get_term_children() is calling _get_term_hierarchy() every time, which on
> the other hand is storing it's data in an option in the options table...
> I guess that might work :)
>
> Alternatively, I can store the last-level terms(once I fetch them) in an
> option as well and flush that option when a term is added/updated/deleted
> from that taxonomy.
>
> Any other ideas?
>
>
> On Mon, May 26, 2014 at 6:19 AM, Haluk Karamete <[hidden email]
> >wrote:
>
> > HI Nikola
> > what if you walk thru all terms in that tax and check if the current term
> > has children or not.
> >
> > http://codex.wordpress.org/Function_Reference/get_term_children
> >
> > Did I understand you correctly?
> > Here's an interesting one - how can I get all of the *last-level* terms
> in
> > a hierarchical taxonomy?
> >
> > Take the following hierarchy for instance:
> >
> >  - Food
> >  - - Vegetables
> >  - - - Cucumber
> >  - - - Carrot
> >  - - Bread
> >  - - Fruits
> >  - - - Citrus Fruits
> >  - - - - Orange
> >  - - - - Pomelo
> >  - Potato
> >
> > ( I know the potato doesn't make sense, but I just wanted to illustrate
> the
> > fact that some terms might be without children )
> >
> > In this case, I'd want to grab the following terms: Cucumber, Carrot,
> > Bread, Orange, Pomelo, Potato.
> >
> > I've been looking around I haven't found an easy solution so far.
> >
> > I can think of two options - getting all terms and then recreating the
> tree
> > from them, or getting each level of terms and doing subsequent
> get_terms()
> > calls(which I don't like, since it's probably going to be very
> > inefficient).
> >
> > My goal is to be able to present to the user a list of all last-level
> terms
> > in the taxonomy and then behind the scenes assign the post to all terms
> > from that tree(so, if you select Carrot, your post would be added to
> > Vegetables and Food).
> > I need to do that, since I have a custom table that stores post to post
> > relations and I need to be able to query for all items in a specific
> > category/list of categories with as little extra requests as
> possible(lots
> > of data is expected to be stored and performance is key).
> >
> > Thanks,
> > Nikola
> > _______________________________________________
> > wp-hackers mailing list
> > [hidden email]
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > _______________________________________________
> > wp-hackers mailing list
> > [hidden email]
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Getting lowest/last-level terms in hierarchical taxonomy

Harry Metcalfe-4
In reply to this post by Nikola Nikolov
You could maybe make a new table that stores details about leaf terms, and update it when terms are modified?

I helped out on a project where performance was also key, where we did the above, and it worked well. To be honest, I don't think WordPress's taxonomies will ever be very good if you need something highly performant - since parent/child relationships are only defined upwards (which is your problem here) and any nontrivial reading or manipulation either requires loads of joins or manipulation of large arrays. Or both.

Harry

Sent from my mobile

<div>-------- Original message --------</div><div>From: Nikola Nikolov <[hidden email]> </div><div>Date:2014/05/26  09:33  (GMT+00:00) </div><div>To: [hidden email] </div><div>Subject: Re: [wp-hackers] Getting lowest/last-level terms in hierarchical
  taxonomy </div><div>
</div>Yeah, that's one my thoughts, but I'm afraid it will cause too many queries
perhaps? Although from looking at the code it seems like
get_term_children() is calling _get_term_hierarchy() every time, which on
the other hand is storing it's data in an option in the options table...
I guess that might work :)

Alternatively, I can store the last-level terms(once I fetch them) in an
option as well and flush that option when a term is added/updated/deleted
from that taxonomy.

Any other ideas?


On Mon, May 26, 2014 at 6:19 AM, Haluk Karamete <[hidden email]>wrote:

> HI Nikola
> what if you walk thru all terms in that tax and check if the current term
> has children or not.
>
> http://codex.wordpress.org/Function_Reference/get_term_children
>
> Did I understand you correctly?
> Here's an interesting one - how can I get all of the *last-level* terms in
> a hierarchical taxonomy?
>
> Take the following hierarchy for instance:
>
>  - Food
>  - - Vegetables
>  - - - Cucumber
>  - - - Carrot
>  - - Bread
>  - - Fruits
>  - - - Citrus Fruits
>  - - - - Orange
>  - - - - Pomelo
>  - Potato
>
> ( I know the potato doesn't make sense, but I just wanted to illustrate the
> fact that some terms might be without children )
>
> In this case, I'd want to grab the following terms: Cucumber, Carrot,
> Bread, Orange, Pomelo, Potato.
>
> I've been looking around I haven't found an easy solution so far.
>
> I can think of two options - getting all terms and then recreating the tree
> from them, or getting each level of terms and doing subsequent get_terms()
> calls(which I don't like, since it's probably going to be very
> inefficient).
>
> My goal is to be able to present to the user a list of all last-level terms
> in the taxonomy and then behind the scenes assign the post to all terms
> from that tree(so, if you select Carrot, your post would be added to
> Vegetables and Food).
> I need to do that, since I have a custom table that stores post to post
> relations and I need to be able to query for all items in a specific
> category/list of categories with as little extra requests as possible(lots
> of data is expected to be stored and performance is key).
>
> Thanks,
> Nikola
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Getting lowest/last-level terms in hierarchical taxonomy

Nikola Nikolov
That's probably going to be the best solution, thanks for the idea.

I think I might go with storing the lowest level terms in an option, since
we're getting close to running out of time for that particular milestone -
that should have very good performance as well.

Thanks everyone for the ideas,
Nikola


On Mon, May 26, 2014 at 1:06 PM, Harry Metcalfe <[hidden email]> wrote:

> You could maybe make a new table that stores details about leaf terms, and
> update it when terms are modified?
>
> I helped out on a project where performance was also key, where we did the
> above, and it worked well. To be honest, I don't think WordPress's
> taxonomies will ever be very good if you need something highly performant -
> since parent/child relationships are only defined upwards (which is your
> problem here) and any nontrivial reading or manipulation either requires
> loads of joins or manipulation of large arrays. Or both.
>
> Harry
>
> Sent from my mobile
>
> <div>-------- Original message --------</div><div>From: Nikola Nikolov <
> [hidden email]> </div><div>Date:2014/05/26  09:33  (GMT+00:00)
> </div><div>To: [hidden email] </div><div>Subject: Re:
> [wp-hackers] Getting lowest/last-level terms in hierarchical
>         taxonomy </div><div>
> </div>Yeah, that's one my thoughts, but I'm afraid it will cause too many
> queries
> perhaps? Although from looking at the code it seems like
> get_term_children() is calling _get_term_hierarchy() every time, which on
> the other hand is storing it's data in an option in the options table...
> I guess that might work :)
>
> Alternatively, I can store the last-level terms(once I fetch them) in an
> option as well and flush that option when a term is added/updated/deleted
> from that taxonomy.
>
> Any other ideas?
>
>
> On Mon, May 26, 2014 at 6:19 AM, Haluk Karamete <[hidden email]
> >wrote:
>
> > HI Nikola
> > what if you walk thru all terms in that tax and check if the current term
> > has children or not.
> >
> > http://codex.wordpress.org/Function_Reference/get_term_children
> >
> > Did I understand you correctly?
> > Here's an interesting one - how can I get all of the *last-level* terms
> in
> > a hierarchical taxonomy?
> >
> > Take the following hierarchy for instance:
> >
> >  - Food
> >  - - Vegetables
> >  - - - Cucumber
> >  - - - Carrot
> >  - - Bread
> >  - - Fruits
> >  - - - Citrus Fruits
> >  - - - - Orange
> >  - - - - Pomelo
> >  - Potato
> >
> > ( I know the potato doesn't make sense, but I just wanted to illustrate
> the
> > fact that some terms might be without children )
> >
> > In this case, I'd want to grab the following terms: Cucumber, Carrot,
> > Bread, Orange, Pomelo, Potato.
> >
> > I've been looking around I haven't found an easy solution so far.
> >
> > I can think of two options - getting all terms and then recreating the
> tree
> > from them, or getting each level of terms and doing subsequent
> get_terms()
> > calls(which I don't like, since it's probably going to be very
> > inefficient).
> >
> > My goal is to be able to present to the user a list of all last-level
> terms
> > in the taxonomy and then behind the scenes assign the post to all terms
> > from that tree(so, if you select Carrot, your post would be added to
> > Vegetables and Food).
> > I need to do that, since I have a custom table that stores post to post
> > relations and I need to be able to query for all items in a specific
> > category/list of categories with as little extra requests as
> possible(lots
> > of data is expected to be stored and performance is key).
> >
> > Thanks,
> > Nikola
> > _______________________________________________
> > wp-hackers mailing list
> > [hidden email]
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> > _______________________________________________
> > wp-hackers mailing list
> > [hidden email]
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers