• Resolved MaryMak

    (@maxms)


    Hello,

    I am at my wit’s end trying to make a custom query work and just want to check and see whether this might have anything to do with the fact that the taxonomy was created with CPT UI and if there need to be any settings changed.

    I need the query below to return posts which match ALL of the same terms (from the custom taxonomy ‘models’) as the current post. As it is, using the ‘AND’ operator returns no posts at all (there should be one on my test page). If I remove that it returns all posts that match ANY of the terms.

    I saw another post that had the same problem with a different type of query and thought I’d check with support here to see if you have any ideas. Thanks!

    add_action( 'elementor/query/model_loop_free', function( $query ) {
    //get ID of current post
    $id = get_queried_object_id();
    // get current taxonomy - in this case the make/model
    $taxos = get_the_terms( $id, 'models');
    foreach ( $taxos as $tax ) {
            $tax_slugs[] = $tax->slug;
        }
    $tax_query = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'models',
            'field' => 'slug',
            'terms' => $tax_slugs,
            'operator' => 'AND'
        ),
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'free-play-adapter'
        ),
    );
    
    $query->set( 'tax_query', $tax_query );
    } );
    • This topic was modified 2 years, 9 months ago by MaryMak.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Shouldn’t have anything to do with how the taxonomies were registered, as we don’t affect queries with the CPTUI code at all.

    Based on what I’m seeing with the code above.

    You’re wanting to find all posts that have ALL of the terms assigned from the models taxonomy of the current post, but also the free-play-adapter term from the product_cat taxonomy, which I assume is from WooCommerce.

    Correct?

    Perhaps try without the product_cat portion and see if you can get just the ones with models.

    Thread Starter MaryMak

    (@maxms)

    Still didn’t work without the product_cat portion. Thanks for the suggestion and for replying so quickly. Have a great day!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not sure, and that’s potentially more a WordPress core limitation than anything CPTUI does. I thought I had it working locally, but then I realized it was querying all that had either of 2 specified terms, not limiting to both.

    Thread Starter MaryMak

    (@maxms)

    Thank you so much for checking! If I figure something out I will let you know.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Sounds good. Hopefully a solution gets found.

    Thread Starter MaryMak

    (@maxms)

    Hi Michael,

    Just checking in. I still haven’t found a solution so I think you’re right about this being a WordPress core limitation. I may mention it to them.

    In the meantime I think we can achieve what we need by just creating additional taxonomies so that it doesn’t have to match more than one term in each.

    Thanks again for your time and advice!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

    Thread Starter MaryMak

    (@maxms)

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Ahhh, that makes sense all in all. Glad they were able to help and get this figured out. Also that it’s not actually a limit from WP Core.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Custom Taxonomy query to match all terms’ is closed to new replies.