Using WP Query to search by multiple meta fields

I made a custom search form with select boxes and text fields.

I have a select box with options like “renting” and “selling” (its a real estate site)
There are also input fields for minimum and maximum price, for location etc.

Read More

In the posts I have custom fields for all those filters.

I used WP_Query for that. And it should work. Here is the query:

// Define the arguments for the WP query
$args = array(
        'post_type' => 'post',
        'relation' => 'AND',
        'meta_query' => array(
                array(
                        'key' => 'ex_lokacija',
                        'value' => $grad ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_vrsta_oglasa',
                        'value' => $adType ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_tip_nekretnine',
                        'value' => $realEstateType ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_dio_pg',
                        'value' => $dioGrada ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_dio_pg',
                        'value' => $dioGrada ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'et_square_footage',
                        'value' => array( $squareFrom, $squareTo ),
                        'type' => 'numeric',
                        'compare' => 'BETWEEN'
                ),
                array(
                        'key' => 'et_price',
                        'value' => array( $priceFrom, $priceTo ),
                        'type' => 'numeric',
                        'compare' => 'BETWEEN'
                ),
                array(
                        'key' => 'et_bedrooms_number',
                        'value' => $roomsNum,
                        'type' => 'numeric',
                        'compare' => 'LIKE'
                )
        )
);

$searched_posts = new WP_Query( $args );

I used preg_replace for each of the $_GET vars and assigned them to these you see in ‘value’.

Now, the problem is that it doesn’t show anything, even if there should be results.

Also, it won’t work without the ‘s’ field – something must be put in it in order to even load the empty results page.

Possible problem #1

I’m guessing there is a problem with the loop, which I created like this:

                    while ($searched_posts->have_posts()) : $searched_posts->the_post(); ?>



                        <?php the_title(); ?>
                        <?php the_content(); ?>



                    <?php endwhile; ?>

That is just for testing.

Possible problem #2

I put all of this inside of one file – search.php.
Maybe that is causing problems?

Here it is:

‚‚

global $wpdb;

$grad           = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['grad'] );
$adType         = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['adType'] );
$realEstateType = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['realEstateType'] );
$dioGrada       = preg_replace( '/^[0-9a-zA-Z-]/', '', $_GET['dioGrada'] );
$squareFrom     = preg_replace( '/[^0-9]/', '', $_GET['squareFrom'] );
$squareTo       = preg_replace( '/[^0-9]/', '', $_GET['squareTo'] );
$priceFrom      = preg_replace( '/[^0-9]/', '', $_GET['priceFrom'] );
$priceTo        = preg_replace( '/[^0-9]/', '', $_GET['priceTo'] );
$roomsNum       = preg_replace( '/[^0-9]/', '', $_GET['roomsNum'] );

// Change the defaults if not chosen
if($squareFrom == '') { $squareFrom = '0'; }
if($squareTo == '') { $squareTo = '10000000'; }

// Define the arguments for the WP query
$args = array(
        'post_type' => 'post',
        'relation' => 'AND',
        'meta_query' => array(
                array(
                        'key' => 'ex_lokacija',
                        'value' => $grad ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_vrsta_oglasa',
                        'value' => $adType ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_tip_nekretnine',
                        'value' => $realEstateType ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_dio_pg',
                        'value' => $dioGrada ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'ex_dio_pg',
                        'value' => $dioGrada ,
                        'compare' => 'LIKE'
                ),
                array(
                        'key' => 'et_square_footage',
                        'value' => array( $squareFrom, $squareTo ),
                        'type' => 'numeric',
                        'compare' => 'BETWEEN'
                ),
                array(
                        'key' => 'et_price',
                        'value' => array( $priceFrom, $priceTo ),
                        'type' => 'numeric',
                        'compare' => 'BETWEEN'
                ),
                array(
                        'key' => 'et_bedrooms_number',
                        'value' => $roomsNum,
                        'type' => 'numeric',
                        'compare' => 'LIKE'
                )
        )
);

$searched_posts = new WP_Query( $args );



 get_header(); ?>

    <div id="content-top">
        <div id="menu-bg"></div>
        <div id="top-index-overlay"></div>

        <div id="content" class="clearfix">
            <div id="main-area">
                <?php get_template_part('includes/breadcrumbs'); 

                    while ($searched_posts->have_posts()) : $searched_posts->the_post(); ?>



                        <?php the_title(); ?>
                        <?php the_content(); ?>



                    <?php endwhile; ?>

            </div> <!-- end #main-area -->

            <?php get_sidebar(); ?>

<?php get_footer(); ?>

Thanks for viewing my problem!

Related posts

Leave a Reply

1 comment

  1. The way you have built that query it is possible for you to have a bunch of empty values. For example, if grad is not present then you end up searching ex_lokacija keys for empty strings. Since your main relation is AND all of the terms have to ‘hit’ so it seems likely to me that it would be very hard to get a query that returns anything. I would build the query to only search keys that have values.

    $meta_query = array();
    if (!empty($grad)) {
        $meta_query[] = array(
             'key' => 'ex_lokacija',
             'value' => $grad ,
             'compare' => 'LIKE'
        )
    }
    // and so on for each of your keys, then
    $args = array(
            'post_type' => 'post',
            'relation' => 'AND',
            'meta_query' => $meta_query
    )
    $searched_posts = new WP_Query( $args );