WordPress: Advanced search for custom post types and custom fields

I have the following problem. I am trying to create a real estate website with wordpress. I have created a custom post type and I included the following fields with Advanced custom fields: Listing type (select: For sale, For rent), Property type (select: Apartment, House), Price (Number), City (field type: select)

I have the following query to get the values:

Read More
<?php
  $args = array(
            'post_type' => 'properties',
            'posts_per_page' => 10,
            'posts_per_page' => -1,
            'meta_query' => array(
                    array(
                        'key'     => 'property_type',
                        'value'   => 'Apartment',
                    ),
                ),
        );
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
  ?>
  <?php
    $property_type = get_field('property_type');
            $property_price = get_field('price');
            $property_location = get_field('location');
            $property_description = get_field('description');
    ?>
    <div class="propert_list">
        <h1><?php the_title(); ?></h1>
        <div class="property_type">
            Location - <strong><?php echo $property_type; ?></strong>
        </div>
        <div class="property_price">
            Price - <strong><?php echo $property_price; ?></strong>
        </div>
        <div class="property_location">
            Location - <strong><?php echo $property_location['address']; ?></strong>
        </div>
        <div class="property_description">
            Property Description - <strong><?php echo $property_description; ?></strong>
        </div>
    </div>
    <br /><hr />
    <?php endwhile; ?>

Where and how can I create my search form to filter properties by ‘property_type’ and ‘location’.

Related posts

3 comments

  1. One solution is to install Search & Filter.
    It allows you to filter the search based on given post type/s, meta field/s etc. and is very powerful. A drawback is that you may have to pay for the Pro version.

    Another solution could be to add hidden input fields to your PHP searchform. E.g:

    <input type="hidden" name="post_type[]" value="properties" />
    

    to only search for the properties, and do the same for filtering your meta values. This is easy to implement, but less flexible and I prefer to separate my view and logic anyway.

    A third solution would be to use the pre_get_posts filter. Then you could write e.g.

    if ( $post_types ) $query->set( 'post_type', 'properties' );
    

    …and so on, inside this filter. There are many examples out there that shows you how to do this. Just search Google for the filter name 🙂

  2. If you wish to make the search under same page template, you can achieve that using searchable HTML data table and JavaScript. Just encapsulate your values in table. You may refer these links for example code:

    http://www.vijayjoshi.org/2011/01/03/searching-text-in-a-html-table-using-jquery/

    and

    https://www.datatables.net/examples/data_sources/js_array.html

    I also noticed that you forgot to reset wp query. It always safe to end with wp_reset_query();

    Hope you this helps.`

Comments are closed.