Zain Shabir
Zain Shabir

Reputation: 310

Custom dropdown filter for blogs not working for multiple filters - Wordpress

I have a wordpress website. In blogs page, I have created select dropdown filters, if i write code for single filter then it's working, If i write code for multiple filters then it's not working.
Page:
enter image description here See filters in front-end. And now, see the code:

$filter_model = $_POST['filter_model'];
$args = array(
        'post_type'  => 'ad',
        'meta_key'     => 'model',
        'meta_value'   => $filter_model,
    );

It's for single filter, if i do this, then filter is working and posts are appearing in front-end.

$filter_model = $_POST['filter_model'];
$filter_year = $_POST['filter_year'];
$args = array(
        'post_type'  => 'ad',
        'meta_query' => array(
                            array(
                                'key'     => 'model',
                                'value'   => $filter_model,
                                'compare' => 'NOT LIKE',
                            ),
                            array(
                                'key'     => 'ad_year',
                                'value'   => $filter_year,
                                'compare' => 'NOT LIKE',
                            ),
                        ),
    );
$query = new WP_Query($args);

It;s for multiple filters, if i do this then filters are not working and posts are not appearing in front-end.
Can you please help me where i am wrong, and how can i filter multiple meta keys. I am stuck.

Upvotes: 0

Views: 387

Answers (1)

Manik Malhotra
Manik Malhotra

Reputation: 632

Please change 'compare' => 'NOT LIKE', to 'compare' => '=',. This should work.

Upvotes: 1

Related Questions