WordPress meta key search

I have some problems with wordpress meta key relations and search parametrs. I can not get this to work:

?meta_value=Dažādi&id=32&cp_city=Cēsis

Search results is only filtering by meta_value, not of cp_city and meta_value.

Read More

cp_city is also meta key value.

But this call is working and filtering:

?meta_value=Pārdod&id=32

I need to filter by two or three parametrs.

Here is functions code to filter.

<?php

if (empty($pilseta)) {
     $args=array(
    'post_type'          => 'ad_listing',
    'post_status'        => 'publish',
    'meta_key'           => 'cp_tips',
    'meta_value'         => $meta,
    'paged'              => $paged,
    'tax_query'          => array(
        array(
            'taxonomy' => 'ad_cat',
            'terms' => $cat_id,      
            'field' => 'id'
        )
    )
);
}
else {
    $args=array(

    'post_type'          => 'ad_listing',
    'post_status'        => 'publish',
    'meta_query' => array(
        array(
            'meta_key'           => 'cp_tips',
            'meta_value'         => $meta
        ),
         array(
            'meta_key'           => 'cp_city',
            'meta_value'         => $pilseta
        ),
    ),
    'paged'              => $paged,
    'tax_query'          => array(
        array(
            'taxonomy' => 'ad_cat',
            'terms' => $cat_id,      
            'field' => 'id'
        )
    )
);
}



query_posts($args);

?>

Related posts

Leave a Reply

2 comments

  1. Does you saw the SQL query generated by WordPress to know if is ok?

    After query_posts call, puts this:

    <?php global $wpdb; echo $wpdb->last_query; ?>
    

    If what WP is writing an unexpected query, I advice you to use posts_where filter with get_posts instead of query_posts

  2. Change the meta_key args file below and try:

    'meta_query' => array(
            array(
                'key' => 'cp_tips',
                'value'=> $meta
            ),
             array(
                'key'=> 'cp_city',
                'value'=> $pilseta
    )