• Sam

    (@forbiddenchunk)


    I have two list of checkboxes, “Animal” and “Colour”

    I’ve written ajax query to filter them down, when I select dog and cat, they only show. However I say I want colour grey, it shows all animals as well that relate to grey.

    Below is my WP query, I can’t work out why it doesn’t filter right….

        // Getting the ajax data:
        // An array of keys("name")/values of each "checked" checkbox
        $experimentChoices = $_POST['experiment'];
        $mediaChoices = $_POST['media'];
    
        $meta_query = array(
            'relation' => 'OR'
        );
        
        foreach($mediaChoices as $Key=>$Value){
            if(count($Value)){
                foreach ($Value as $Inkey => $Invalue) {
                    $meta_query[] = array( 
                        'key' => $Key, 
                        'value' => $Invalue, 
                        'compare' => '=' 
                    );
                }
            }
        }
    
        foreach($experimentChoices as $Key=>$Value){
            if(count($Value)){
                foreach ($Value as $Inkey => $Invalue) {
                    $meta_query[] = array( 
                        'key' => $Key, 
                        'value' => $Invalue, 
                        'compare' => '=' 
                    );
                }
            }
        }
    
        $args = array(
            'post_type' => 'downloads',
            'meta_query' =>$meta_query
        );
    
        $query = new WP_Query($args);
    
        if( $query->have_posts() ) :
        while( $query->have_posts() ): $query->the_post();
            get_template_part( 'template-parts/filter-content' );
        endwhile;
            wp_reset_query();
        else :
            // wp_send_json($query->posts);
            print '<div class="post_content"><p>Sorry no publications found</p></div>';
        endif;
    
        die();
Viewing 5 replies - 1 through 5 (of 5 total)
  • 'relation' => 'OR'

    Be sure to include some sanitization of the user inputs or your site will be hacked.

    Thread Starter Sam

    (@forbiddenchunk)

    Hi @joyously

    Thanks for the reply, I tried that and it didn’t work. I think my foreach loop is outputting my array wrong;

    foreach($experimentChoices as $Key=>$Value){
            if(count($Value)){
                foreach ($Value as $Inkey => $Invalue) {
                    $exp_query[] = array( 
                        'key' => $Key, 
                        'value' => $Invalue, 
                        'compare' => '=',
                    );
                }
            }
        }

    This is why it outputs;

    FOREACH (broke)

    Array ( [0] => Array ( [key] => experiment_type [value] => abuse-dependence [compare] => = ) [1] => Array ( [key] => experiment_type [value] => diabetes [compare] => = ) )

    WRITTEN CODE (working)

    Array ( [relation] => OR [0] => Array ( [key] => experiment_type [value] => abuse-dependence [compare] => = ) [1] => Array ( [key] => experiment_type [value] => diabetes [compare] => = ) )

    and will add sanitization thanks

    Moderator bcworkz

    (@bcworkz)

    The broken one for is downloads where “experiment_type” meta field equals both “abuse-dependence” AND “diabetes”. This seems to be a impossible situation. The field cannot equal two different values at the same time, right?

    Default relation is AND. Specifying OR makes sense, requiring both at the same time is impossible.

    Thread Starter Sam

    (@forbiddenchunk)

    Hi @bcworkz thanks for the reply.

    So how would you do it so the user can select for example,

    Abuse OR diabetes from “experiment_type”

    AND

    Posters OR Journals from “media_type”?

    Which then the filter would only show anything that fits those 4 parameters

    Moderator bcworkz

    (@bcworkz)

    The meta_query array structure, in a pseudo code:

    [ [
         key => experiment_type
         value => [Abuse, diabetes]
         compare => 'IN'
       [
       [
         key => media_type
         value => [Posters, Journals]
         compare => 'IN'
       ]
    ]

    That’s the gist of it, I leave the proper syntax to you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Ajax filter not querying correctly’ is closed to new replies.