• I have a custom post with a nested taxonomy. Is it possible, preferably through WP_Query, but even with sql if not, to get all posts that exactly match the parent taxonomy given, and LIKE match the child given?

    For instance, my post is product_cpt. I have a description_ct that is parent/child. So descriptions can be (x means checked):
    x-CATS
    -x-Molly
    -x-Patrick
    -x-Pam
    -Papi
    x-DOGS
    -Buster
    -x-Pam
    -Sally
    x-PARROTS
    -x-Dolly

    I want to be able to search for products that only match CATS with children LIKE “Pa”. So it would only return a product that had CATS->Pam or CATS->Patrick selected. But not products with DOGS->Pam selected. And not products with PARROTS selected.
    So it’s really important that I know the “Pa” is only searching for “Pa”‘s that fall under CATS, and not “Pa”‘s found anywhere else.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot use WP_Query for that unless you override the final SQL just before the query is run with the filter “posts_request”. If you are going to do that, IMO you may as well write your own SQL and use $wpdb methods (yes, it’s possible). It’s more “honest” in a way. The only reason to use the filter is if you need to modify a WP_Query created through another process. If you would be starting your own new WP_Query, don’t bother, just use $wpdb instead.

    Thread Starter bjacobs09

    (@bjacobs09)

    Thank you very much for your answer. I will try the $wpdb functions to query.

    Thread Starter bjacobs09

    (@bjacobs09)

    P.S. Is it not possible with WP_Query bc of the LIKE match as opposed to an exact match? Or is it not possible because it’s a nested taxonomy?

    Moderator bcworkz

    (@bcworkz)

    WP_Query might be able to handle nested taxonomies, it depends on how the terms are assigned to posts. If the desired posts always have the parent term, then it’s doable with fully specified terms. LIKE queries are not supported.

    Thread Starter bjacobs09

    (@bjacobs09)

    Yes I added an action on the save_post to always select the parent on save. So a child selected will always have the parent selected. However, there may be a parent selected without having a child selected.

    Moderator bcworkz

    (@bcworkz)

    That’s not a problem. Either way you can use WP_Query with the “tax_query” argument to specify whatever terms are required. It will not accept LIKE arguments though.

    I suppose you could get all posts that match the parent, then with PHP, get the terms of each post and determine if any terms are LIKE the child argument, discarding non-matches. Not very efficient, but feasible for smaller sites.

    Thread Starter bjacobs09

    (@bjacobs09)

    I just did a $wpdb query and it worked perfectly. Thanks for your help.

    Moderator bcworkz

    (@bcworkz)

    Good to hear. Happy to help ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Querying a post by taxonomy parent exact and child LIKE’ is closed to new replies.