1

I wanted to make custom search with multiple fields in which we have to filter by post title and with other custom fields and taxonomies.

have a look on my code

Form

<form role="search" method="get" id="searchform" action="<?php echo home_url('/'); ?>">
    <div class="row whitebg">
        <div class="inner hideshowinner" style="display:block;">
            <div class="search-main">
                <div class="heading">
                    <h3>
                        All Developments
                    </h3>
                </div>
                <ul class="hideshow">
                    <li>
                        <?php
                        // $terms = get_terms(array(
                        //     'taxonomy' => 'projectcategory',
                        //     'hide_empty' => false
                        // ));
                        // if (! empty($terms) && ! is_wp_error($terms)) {
                        //     echo '<select required class="project projectsearch" name="s"><option value="">Project Name</option>';
                        //     foreach ($terms as $term) {
                        //         echo '<option value="'. esc_attr($term->term_id) .'">' . $term->name . '</li>';
                        //     }
                        //     echo '</select>';
                        // }
                        echo '<select required class="project projectsearch" name="s"><option value="">Project Name</option>';
                        query_posts(array('post_type'  => 'property'));
                        if (have_posts()) : while (have_posts()) : the_post(); ?>
                                        <option value="<?php the_title();?>"><?php the_title();?></option>
                                        <?php
                            endwhile;
                            else :  endif;
                            wp_reset_query();
                        echo '</select>';
                        ?>
                        <?php /*?>
                        <select class="project" name="s">
                            <option value="">Project</option>
                            <?php $index_query = new WP_Query(array( 'post_type' => 'property', 'showposts' => -1,'order'=>'DESC' )); ?>
                            <?php while ($index_query->have_posts()) : $index_query->the_post(); ?>
                            <option value="<?php the_title(); ?>"><?php the_title(); ?></option>
                            <?php endwhile; wp_reset_postdata(); ?>
                        </select>
                        <?php */ ?>
                    </li>

                    <li>
                        <div class="formset">
                            <input type="submit" id="searchsubmit" value="Search" />
                        </div>
                    </li>
                </ul>
            </div>
            <div class="wrapped">
                <ul class="hideshow">
                    <li>

                        <?php $ccountrie_query = new WP_Query(array('post_type' => 'property', 'showposts' => -1, 'order' => 'DESC'));
                        while ($ccountrie_query->have_posts()) : $ccountrie_query->the_post();
                            if (get_field('ccountries') != null) {
                                $ccountries[] = get_field('ccountries');
                            }
                        ?>
                        <?php endwhile;
                        wp_reset_postdata();
                        $ccountries = array_unique($ccountries);
                        // Removes duplicates;
                        echo '<select id="country" name="country"><option value="">Country</option>';
                        foreach ($ccountries as $ccountrie) {
                            echo '<option value=' . $ccountrie . '>' . $ccountrie . '</option>';
                        }
                        echo '</select>'; ?>

                        <?php /*
                        <select id="country" name="country">
                            <option value="">Country</option>
                            <option value="AE">United Arab Emirates</option>
                            <option value="TR">Turkey</option>
                            <option value="OM">Oman</option>
                            <option value="PK">Pakistan</option>
                            <option value="GE">Georgia</option>
                            <option value="GB">United Kingdom</option>
                        </select> */ ?>
                    </li>
                    <li>
                        <?php $locations_query = new WP_Query(array('post_type' => 'property', 'showposts' => -1, 'order' => 'DESC'));
                        while ($locations_query->have_posts()) : $locations_query->the_post();
                            if (get_field('location') != null) {
                                $locations[] = get_field('location');
                            }
                        ?>
                        <?php endwhile;
                        wp_reset_postdata();
                        $locations = array_unique($locations);
                        // Removes duplicates;
                        echo '<select name="location" class="location"><option value="">Areas</option>';
                        foreach ($locations as $location) {
                            echo '<option value=' . $location . '>' . $location . '</option>';
                        }
                        echo '</select>'; ?>

                        <?php /*<select name="location" class="location">
                            <option value="">Location</option>
                            <option value="2000">2,000 </option>
                            <option value="4000">4,000</option>
                            <option value="6000">6,000</option>
                            <option value="8000">8,000</option>
                        </select>*/ ?>
                    </li>
                    <li>
                        <select class="developer" name="eveloper">
                            <option value="">Developer</option>
                            <?php $index_query = new WP_Query(array('post_type' => 'developer', 'showposts' => -1, 'order' => 'DESC')); ?>
                            <?php while ($index_query->have_posts()) : $index_query->the_post(); ?>
                            <option value="<?php the_title(); ?>"><?php the_title(); ?></option>
                            <?php endwhile;
                            wp_reset_postdata(); ?>
                        </select>
                    </li>
                    <li>
                        <select class="propertytype" name="propertytype">
                            <option value="">Property Type</option>
                            <option value="Apartments">Apartments</option>
                            <option value="Villas">Villas</option>
                            <option value="Town Houses">Town Houses</option>
                            <option value="Hotel Apartments">Hotel Apartments</option>
                            <option value="Lands">Lands</option>
                            <option value="Shops">Shops</option>
                            <option value="Offices">Offices</option>
                        </select>
                    </li>
                </ul>
                <ul class="hideshow scnd">
                    <li>
                        <?php $hyear_query = new WP_Query(array('post_type' => 'property', 'showposts' => -1, 'order' => 'DESC'));
                        while ($hyear_query->have_posts()) : $hyear_query->the_post();
                            if (get_field('handover_year') != null) {
                                $handover_quarters[] = get_field('handover_year');
                            }
                        ?>
                        <?php endwhile;
                        wp_reset_postdata();
                        $hyears = array_unique($handover_quarters);
                        // Removes duplicates;
                        echo '<select class="handover" name="handover"><option value="">Hand Over</option>';
                        foreach ($hyears as $hyear) {
                            echo '<option value=' . $hyear . '>' . $hyear . '</option>';
                        }
                        echo '</select>'; ?>

                        <?php /*<select class="handover" name="handover">
                            <option value="">Hand Over</option>
                            <?php
                        $firstYear = (int)date('Y');
                        $lastYear = $firstYear + 10;
                        for($i=$firstYear;$i<=$lastYear;$i++)
                        {
                            echo '<option value='.$i.'>'.$i.'</option>';
                        }?>
                        </select>*/ ?>
                    </li>
                    <li>
                        <select name="min_price" class="min-p">
                            <option value="">Min Price (AED)</option>
                            <option value="50000">500,000 </option>
                            <option value="750000">750,000</option>
                            <option value="1000000">1,000,000</option>
                            <option value="1500000">1,500,000</option>
                            <option value="2000000">2,000,000</option>
                            <option value="2500000">2,500,000</option>
                            <option value="3000000">3,000,000</option>
                            <option value="3500000">3,500,000</option>
                            <option value="5000000">5,000,000</option>
                            <option value="4500000">4,500,000</option>
                            <option value="5000000">5,000,000</option>
                            <option value="6000000">6,000,000</option>
                            <option value="6000000">6,000,000</option>
                            <option value="8000000">8,000,000 </option>
                            <option value="9000000">9,000,000</option>
                            <option value="10000000">10,000,000</option>
                            <option value="15000000">15,000,000</option>
                            <option value="20000000">20,000,000</option>
                            <option value="25000000">25,000,000</option>
                            <option value="50000000">50,000,000</option>
                        </select>
                    </li>
                    <li>
                        <select name="max_price" class="max-p">
                            <option value="">Max Price (AED)</option>
                            <option value="50000">500,000 </option>
                            <option value="750000">750,000</option>
                            <option value="1000000">1,000,000</option>
                            <option value="1500000">1,500,000</option>
                            <option value="2000000">2,000,000</option>
                            <option value="2500000">2,500,000</option>
                            <option value="3000000">3,000,000</option>
                            <option value="3500000">3,500,000</option>
                            <option value="5000000">5,000,000</option>
                            <option value="4500000">4,500,000</option>
                            <option value="5000000">5,000,000</option>
                            <option value="6000000">6,000,000</option>
                            <option value="6000000">6,000,000</option>
                            <option value="8000000">8,000,000 </option>
                            <option value="9000000">9,000,000</option>
                            <option value="10000000">10,000,000</option>
                            <option value="15000000">15,000,000</option>
                            <option value="20000000">20,000,000</option>
                            <option value="25000000">25,000,000</option>
                            <option value="50000000">50,000,000</option>
                        </select>
                    </li>
                    <li>
                        <select name="bedroom" class="bedroom">
                            <option value="">Select Bedroom</option>
                            <option value="0">0</option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                        </select>
                    </li>
                </ul>
            </div>
            <label>More Options</label>
            <a href="#" id="configreset"> Reset</a>
        </div>
    </div>
</form>

and my search.php file code is

    <?php 
$sval = $_GET['s'];
    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        
        if ($_GET['s'] !=NULL) {

            $services_query = new WP_Query(
                 
                    array(
                        'post_type' => 'property',
                            'title' => $_GET['s'],
                            
                        
                        'meta_query' => array(
                        
                            'relation' => 'OR',
                            
                            array(
                                'key'     => 'ccountries',
                                'value' => $_GET['country'],
                                'compare' => '=',
                            ),
                            
                            array(
                                'key'     => 'sqt',
                                'value' => $_GET['area'],
                                'compare' => '<=',
                            ),
                            array(
                                'key'     => 'developer_name',
                                'value' => $_GET['eveloper'],
                                'compare' => 'EXISTS',
                            ),
                            array(
                                'key'     => 'property_type',
                                'value' => $_GET['propertytype'],
                                'compare' => '=',
                            ),
                            array(
                                'key'     => 'handover_year',
                                'value' => $_GET['handover'],
                                'compare' => '=',
                            ),
                            array(
                                'key'     => 'bedroom',
                                'value' => $_GET['bedroom'],
                                'compare' => '=',
                            ),
                            array(
                                    'key'     => 'price',
                                    'value'   => array( $_GET['min_price'], $_GET['max_price'] ),
                                    'compare' => 'BETWEEN',
                            ),
                            
                      ),
                              
                      
                      'posts_per_page' => -1,
                      'paged' => $paged,
                      'order' => 'ASC'
                    )
                );
          /* Property Type with Min and Max Price */
                
                
        
        } ?>
    <?php if ($services_query->have_posts()) : 
            ?>
    <div class="row">

        <?php while ($services_query->have_posts()) : $services_query->the_post(); ?>

        <div class="col-md-6 item">
            <div class="single-property-item1">
                <a href="<?php the_permalink(); ?>">
                    <div class="property-content-top1">
                        <div class="row">
                            <div class="col-md-12">
                                <h2 title="<?php the_title(); ?>">
                                    <?php $name = get_the_title();
                                    if (strlen($name) <= 25) {
                                        echo $name;
                                    } else {
                                        echo substr($name, 0, 25) . '...';
                                    }
                                    ?></h2>
                                <h3><i class="fa fa-map-marker" aria-hidden="true"></i><?php the_field('location'); ?></h3>
                            </div>
                        </div>
                    </div>
                    <div class="property-details" style="background: url(<?php the_post_thumbnail_url('medium_large'); ?>)">
                    </div>
                </a>
                <div class="row property-detail-icon">
                    <div class="col-md-3">
                        <li><img src="./wp-content/uploads/2020/10/property-detail-icon1.png"></li>
                        <li><?php the_field('bedroom'); ?> Bedroom(s)</li>
                    </div>
                    <div class="col-md-3">
                        <li><img src="./wp-content/uploads/2020/10/property-detail-icon2.png"></li>
                        <li><?php the_field('bathroom'); ?> Bathroom(s)</li>
                    </div>
                    <div class="col-md-3">
                        <li><img src="./wp-content/uploads/2020/10/property-detail-icon3.png"></li>
                        <li><?php if ($sqt = get_field('sqt')) {
                                echo $sqt . ' sqft';
                            } else { ?>SQFT N/A <?php } ?></li>
                    </div>
                    <div class="col-md-3">
                        <li>
                            <?php
                            $featured_posts = get_field('developer_name');
                            if ($featured_posts) :
                                foreach ($featured_posts as $featured_post) :
                                    $thumbnails = get_the_post_thumbnail_url($featured_post->ID, 'full');
                                    // Setup this post for WP functions (variable must be named $post).
                                    setup_postdata($posts);
                            ?>
                                    <img src="<?php echo $thumbnails; ?>" alt="">
                            <?php
                                endforeach;
                                // Reset the global post object so that the rest of the page works correctly.
                                wp_reset_postdata();
                            endif;
                            ?>
                        </li>
                    </div>


                </div>
                <div class="row property-content-btm1">
                    <div class="col-md-8">
                        <?php $price =  get_field('price');
                        if ($price) : ?>
                            <label class="starting-from">Starting From</label>
                            <h3>AED <?php echo number_format($price); ?></h3>

                        <?php else : echo "<h3>ASK FOR PRICE</h3>"; ?>


                        <?php endif; // end of if field_name logic 
                        ?>

                    </div>
                    <div class="col-md-4">
                        <div class="property-icon1-1">
                            <li><a href="tel:<?php echo $phonenumber ?>"><i style="transform: rotate(90deg);" class="fa fa-phone" aria-hidden="true"></i></a></li>
                            <li><a href="mailto:<?php echo $emailadd ?>"><i class="fa fa-envelope" aria-hidden="true"></i></a></li>
                            <li><a href="https://api.whatsapp.com/send?phone=<?php echo $wpnumber ?>"><i class="fa fa-whatsapp" aria-hidden="true"></a></i></li>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <?php endwhile; wp_reset_query(); ?>

        <?php if (function_exists("pagination")) {
                    echo '<div class="col-md-12 paginationdiv">';
                    pagination($services_query->max_num_pages);
                    echo '</div>';
                } ?>
        <?php else: ?>
        <div id="post-0" class="post no-results not-found">
            <h2 class="entry-title"><?php _e( 'Nothing Found', 'twentyten' ); ?></h2>
            <div class="entry-content">
                <p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentyten' ); ?>
                </p>
                <?php get_search_form(); ?>
            </div><!-- .entry-content -->
        </div><!-- #post-0 -->
        <?php endif; ?>
    </div>
  • List item
1
  • So what is the question? is something not working as expected? you copy/pasted two big chuncks of code without any explanation as to what needs to be done. You just said that you wanted ......, provide more information about what is the problem. Aug 8 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.