Support

Account

Home Forums General Issues WP Archive page with filter (key=array, value=array)

Solved

WP Archive page with filter (key=array, value=array)

  • Hi guys! Faced a problem, I can’t solve it the second day. Help me please.

    I made a filter, it worked. But it took a changes and it’s not working now.

    I have post type projects. There are several custom fields. Type, area, color. When there was one color, there were no problems. But now 1-3 colors can be assigned to one project.

    Here is my code

    
    add_action('pre_get_posts', 'my_pre_get_posts');
    
    function my_pre_get_posts( $query) {
    
        if( is_admin() ) {
            return;
        }
    
        if ( ! $query->is_main_query() ) {
            return $query;
        }
    
        $meta_query = $query->get('meta_query');
    
        $colors = explode(',', $_GET['color']);
        $types = explode(',', $_GET['type']);
    
        foreach ($colors as $color){
                $meta_query[] = array(
                    'relation'      => 'AND',
                    array(
                        'key' => 'project_type',
                        'value' => $types,
                        'compare' => 'IN',
                    ),
                    array(
                        'key' => 'project_area',
                        'value' => $_GET['area_min'],
                        'type'      => 'DECIMAL',
                        'compare' => '>',
                    ),
                    array(
                        'key' => 'project_area',
                        'value' => $_GET['area_max'],
                        'type'      => 'DECIMAL',
                        'compare' => '<',
                    ),
                    array(
                        'key' => 'project_color', // array
                        'value' => $color, //array too (if 'value' => '"'.$color.'"' I get the same problem
                        'compare' => 'LIKE',
                    ),
                );  
           }        
        }
    
        $query->set('meta_query', $meta_query);
    
        return;
    
    }
    

    If one color is selected, it works fine. If I choose two or more colors, then no project is suitable for the request.

    Only those projects are assigned to which all the colors that the user has selected are assigned. But it is necessary that all projects that contain at least one of these colors be found.

    How can I make queries so that each foreach result is related to subsequent ones through ‘relation’ => ‘OR’. And at the same time in the request where I check the type, area and color relation remained AND.

    How can I do that? Or is this approach wrong, and need to be done differently? I will be very grateful for your help!

  • You need to create a sub query in the meta query

    
    $sub_query = array('relation' => 'OR');
    foreach ($colors as $color) {
      $sub_query[] = array(
        'key' => 'project_color',
        'value' => '"'.$color.'"',
        'compare' => 'LIKE'
      }
    }
    $meta_query[] = $sub_query;
    

    You may still run into issues. If some of your posts have values stored the old way then this will not work since the values are probably not stored as arrays.

    Also if your list of colors to look for gets too large the number of “LIKE” queries could time out the DB request.

  • Thank! I was just looking for this! Just can’t understand how to correctly insert this code into my code? Can you tell me please?

  • 
    $meta_query = array('relation' => 'AND' /* and other base meta query settings */);
    $sub_query = array('relation' => 'OR');
    foreach ($colors as $color) {
      $sub_query[] = array(
        'key' => 'project_color',
        'value' => '"'.$color.'"',
        'compare' => 'LIKE'
      }
    }
    $meta_query[] = $sub_query;
    
  • Hello! Thank John so so so much! Everything works as it should!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘WP Archive page with filter (key=array, value=array)’ is closed to new replies.