• Hello my WordPress friends,

    I am developing with WP since, I would say a bit more then 14 months now.

    Since a couple of days, i have the awesome ToDo, to create a Portfolio with an Ajax request, and I am stuck …

    Facts:
    I have 3 different taxonomies:
    ‘taxonomy’ => ‘auftragsgrafik_slug_list’,
    ‘field’ => ‘slug’,
    ‘terms’ => $typefilter,

    ‘taxonomy’ => ‘thema’,
    ‘field’ => ‘slug’,
    ‘terms’ => $themefilter,

    ‘taxonomy’ => ‘auftragsgrafik_complexity’,
    ‘field’ => ‘slug’,
    ‘terms’ => $complexityfilter,

    I try to explain it “short”..

    3 Dropdowns, each of it has a bunch of values.
    Lets say:
    .) Dropdown A (‘auftragsgrafik_slug_list‘)
    –> TestA
    –> TestB
    –> TestC
    .) Dropdown B (‘thema‘)
    –> LunchA
    –> LunchB
    –> LunchC
    –> LunchD
    –> LunchE
    –> LunchF
    .) Dropdown C (‘auftragsgrafik_complexity‘)
    –> CoffeeA
    –> CoffeeB
    –> CoffeeC
    –> CoffeeD
    –> CoffeeE
    (u got the Idee)

    If now “TestA” is selected, I just want all Post results where the Taxonomy is linked with TestA.
    If TestA and LunchA is selected, I want only the Post results where both “TestA AND LunchA” are linked via taxonomies to the posts!
    If TestA, LunchA and CoffeeA is selected, I just want Post results where all 3 Taxonomies match with posts!

    I googled, tried, worked, but I found no working Solution for me :/

    My actuall work looks kinda like this:

    I just post the code for 1 Request here, to avoid a bit of spam!

    		
    $args = array(
    
    	'posts_per_page'	=> $entries,
    	'meta_query' 		=> array(array('key' => '_thumbnail_id')),
    	'post_type'		=> 'auftragsgrafik_post',
    	'post_status'		=> 'publish',
    	'tax_query'			=> array(
    		array(
    			'relation'		=> 'OR',
    			array(
    				'taxonomy'	=> 'auftragsgrafik_slug_list',
    				'field'		=> 'slug',
    				'terms'		=> $typefilter,
    				'relation'	=> 'OR',
    			),
    			array(
    				'taxonomy'	=> 'thema',
    				'field'		=> 'slug',
    				'terms'		=> $themefilter,
    				'relation'	=> 'OR',
    			),
    			array(
    				'taxonomy'	=> 'auftragsgrafik_complexity',
    				'field'		=> 'slug',
    				'terms'		=> $complexityfilter,
    			)
    		),
    		'relation'	=> 'OR',
    		array(
    			'taxonomy'	=> 'auftragsgrafik_slug_list',
    			'field'		=> 'slug',
    			'terms'		=> $typefilter,
    			'relation'	=> 'AND',
    			array(
    				'taxonomy'	=> 'thema',
    				'field'		=> 'slug',
    				'terms'		=> $themefilter,
    			),
    		),
    		'relation'	=> 'OR',
    		array(
    			'taxonomy'	=> 'auftragsgrafik_slug_list',
    			'field'		=> 'slug',
    			'terms'		=> $typefilter,
    			'relation'	=> 'AND',
    			array(
    				'taxonomy'	=> 'auftragsgrafik_complexity',
    				'field'		=> 'slug',
    				'terms'		=> $complexityfilter,
    			),
    		),
    	),
    );
    

    I definitely know that this can not work, I spend to much time now to know it… so I need some Guidance here my friends :-/
    PLEASE

    What is the best way to make it work with my specific requirements?
    I also tried it to make multiple “args” and merge those together, but sadly I don’t get it to work..

    I hope you have the Knowledge, in which I am leaking… hope you can help me ??

    Nice greetings, Raphael

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

    (@bcworkz)

    If you want all selections to match, you must use relation AND in you tax_query. The relation argument is only allowed in the outer array, not in the inner criteria arrays. Use the operator argument to apply logic to the criteria specified within that array. To match a single term you may omit the operator argument, or specify “IN”.

    You will need to conditionally add the inner taxonomy argument arrays depending upon which dropdown selections are submitted. You don’t want to have empty terms arguments.

    Thread Starter raphaelo

    (@raphaelo)

    Thanks for your answer, but at all I only learned here that the AND condition is only allowed in the outer array, thanks for that btw!

    I already tried to apply a, logic criteria to my “arg” list..

    I don’t have a problem with getting a match for a single term, I just have problems with all these special conditions..

    i spent yesterday and the day before to many hours in it..
    I don’t get further with my query -.-

    Is it good to work with just 1 “arg” list?
    oder should I create for each condition an extra “arg” list?

    Guys, I need help here and really hope you can help me with this..

    Nice greetings
    R

    Moderator bcworkz

    (@bcworkz)

    Only 1 arg list per query. You could do 3 separate queries, but that is an implicit OR logic between the queries. You want posts that match all selected terms, (using AND logic) don’t you? With 3 queries, you end up with 3 groups of results. If that is the ordering you desire anyway, then it’s worth considering since ordering by taxonomy isn’t supported by WP_Query.

    If you can successfully get single term results through tax_query, it’s a matter of adding similar inner arrays within the outer tax_query array. Your OP code has too many arrays which are likely conflicting with each other. While it is possible to nest arrays to get more complex logic between terms, from your example I don’t see that as necessary. Not to mention the fact you’ve combined arrays incorrectly. Since it’s unnecessary and difficult to explain nesting arrays, I’m not going to bother and focus solely on your example.

    If all 3 terms were required, it would be easier. Since each is optional, each term’s array needs to be conditionally added. A quick, untested example:

    $args = [/*everything but tax_query defined here*/];
    $args['tax_query'] = ['relation'=>'AND',]
    $taxes = ['test','lunch','coffee',];
    foreach ( $taxes as $tax ) {
        if ( 'none' != $_POST[ $tax ]) {    //'none' is first item in unselected dropdown
            $term = sanitize_text_field( stripslashes( $_POST[ $tax ]));
            $args['tax_query'][] = [
                'taxonomy'=> $tax,
                'field'   =>'slug',
                'terms'   => [ $term,],
            ]
        }
    }
    if ( 1 == count( $args['tax_query'])) unset $args['tax_query']; //Nothing selected, get unqualified list

    Note the empty square brackets in $args['tax_query'][], we’re adding additional elements within the outer ‘tax_query’ array. In case you didn’t know,$x = ['string',];as used above is shortcut for$x = array('string',);.

    If the user selected 2 items, the resulting $args array would look something like this:

    $args = array(
        /*everything but tax_query defined here*/
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'test',
                'field'    => 'slug',
                'terms'    => array( 'testa' ),
            ),
            array(
                'taxonomy' => 'coffee',
                'field'    => 'slug',
                'terms'    => array( 'coffeec' ),
            ),
        ),
    );
    • This reply was modified 5 years, 3 months ago by bcworkz.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query with strange multiple conditions’ is closed to new replies.