Help with multiple dropdown tags search

I’m trying to build a search form that allows the user to choose certain post tags and filter through real estate listings. So far I have the form built but when I click the search button it brings me to the home page of the site. The url of the site then changes to the normal url with this added to the end showing what criteria is being used:
/?locations=-1&property-type=46&sqft=-1&beds=-1&baths=-1&garage=-1

I’m guessing what I need is to write some sort of post action in PHP that determines what is being searched and creates a page with the search results. I’m just completely lost on how to do this. I have little database experience and know nothing about connecting to WordPress variables with PHP.

I’m sure there has to be a simpler way to do this, I just haven’t found it yet even after searching around for days. Please help me out here and point me to any helpful resources or whatever you can do.

Here’s how I built the form, adapted from http://codex.wordpress.org/Function_Reference/wp_dropdown_categories:

EDIT: this code is in my searchform.php file and I’m calling it on every page of the site

<form role="search" method="get" id="searchform" action="<?php echo home_url( "https://wordpress.stackexchange.com/" ); ?>">
      <div>
        <!--<label class="screen-reader-text" for="s">Search for:</label>
        <input type="text" value="" name="s" id="s" /> -->
        <?php //wp_dropdown_categories( 'taxonomy=post_tag' ); ?>
        <h3 id="searchlabel">Search Listings</h3>
        <?php 
        //this one shows the locations
        $args = array(
                'taxonomy'           => 'post_tag',
                'show_option_all'    => '',
                'show_option_none'   => 'Any',
                'orderby'            => 'name', 
                'order'              => 'ASC',
                'show_count'         => 0,
                'hide_empty'         => 1, 
                'child_of'           => 0,
                'exclude'            => '-46,-33,-6,-7,-41,-12,-13,-40,-37,-38,-39,-8,-9,-10,-11,-42,-43,-44,-45', 
                'echo'               => 1,
                'selected'           => 0,
                'hierarchical'       => 0, 
                'name'               => 'locations',
                'id'                 => '',
                'class'              => 'postform',
                'depth'              => 0,
                'tab_index'          => 0,
                'hide_if_empty'      => false
            );


            //property type
            $args2 = array(
                'taxonomy'           => 'post_tag',
                'show_option_all'    => '',
                'show_option_none'   => 'Any',
                'orderby'            => 'name', 
                'order'              => 'ASC',
                'show_count'         => 0,
                'hide_empty'         => 1, 
                'child_of'           => 0,
                'exclude'            => '-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-37,-38,-39,-8,-9,-10,-11,-42,-43,-44,-45', 
                'echo'               => 1,
                'selected'           => 0,
                'hierarchical'       => 0, 
                'name'               => 'property-type',
                'id'                 => '',
                'class'              => 'postform',
                'depth'              => 0,
                'tab_index'          => 1,
                'hide_if_empty'      => false
            );

            //sq ft
            $args3 = array(
                'taxonomy'           => 'post_tag',
                'show_option_all'    => '',
                'show_option_none'   => 'Any',
                'orderby'            => 'name', 
                'order'              => 'ASC',
                'show_count'         => 0,
                'hide_empty'         => 1, 
                'child_of'           => 0,
                'exclude'            => '-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-37,-38,-39,-8,-9,-10,-11', 
                'echo'               => 1,
                'selected'           => 0,
                'hierarchical'       => 0, 
                'name'               => 'sqft',
                'id'                 => '',
                'class'              => 'postform',
                'depth'              => 0,
                'tab_index'          => 2,
                'hide_if_empty'      => false
            );

            //beds
            $args4 = array(
                'taxonomy'           => 'post_tag',
                'show_option_all'    => '',
                'show_option_none'   => 'Any',
                'orderby'            => 'name', 
                'order'              => 'ASC',
                'show_count'         => 0,
                'hide_empty'         => 1, 
                'child_of'           => 0,
                'exclude'            => '-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-37,-38,-39,-42,-43,-44,-45', 
                'echo'               => 1,
                'selected'           => 0,
                'hierarchical'       => 0, 
                'name'               => 'beds',
                'id'                 => '',
                'class'              => 'postform',
                'depth'              => 0,
                'tab_index'          => 3,
                'hide_if_empty'      => false
            ); 

            //baths
            $args5 = array(
                'taxonomy'           => 'post_tag',
                'show_option_all'    => '',
                'show_option_none'   => 'Any',
                'orderby'            => 'name', 
                'order'              => 'ASC',
                'show_count'         => 0,
                'hide_empty'         => 1, 
                'child_of'           => 0,
                'exclude'            => '-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-37,-38,-39,-8,-9,-10,-11,-42,-43,-44,-45', 
                'echo'               => 1,
                'selected'           => 0,
                'hierarchical'       => 0, 
                'name'               => 'baths',
                'id'                 => '',
                'class'              => 'postform',
                'depth'              => 0,
                'tab_index'          => 4,
                'hide_if_empty'      => false
            );

            //garage size
            $args6 = array(
                'taxonomy'           => 'post_tag',
                'show_option_all'    => '',
                'show_option_none'   => 'Any',
                'orderby'            => 'name', 
                'order'              => 'ASC',
                'show_count'         => 0,
                'hide_empty'         => 1, 
                'child_of'           => 0,
                'exclude'            => '-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-8,-9,-10,-11,-42,-43,-44,-45', 
                'echo'               => 1,
                'selected'           => 0,
                'hierarchical'       => 0, 
                'name'               => 'garage',
                'id'                 => '',
                'class'              => 'postform',
                'depth'              => 0,
                'tab_index'          => 5,
                'hide_if_empty'      => false
            );

        wp_dropdown_categories($args);
        wp_dropdown_categories($args2);
        wp_dropdown_categories($args3);
        wp_dropdown_categories($args4);
        wp_dropdown_categories($args5);
        wp_dropdown_categories($args6);     
        ?>

        <input type="submit" id="searchsubmit" value="Search" />
      </div>
    </form>

Code for search.php:

<?php
/*
Template Name: Search Results Page
*/

get_header(); ?>


<div id="contentWrap">
    <div id="wholeSidebar">
        <?php get_sidebar(); ?>
        <img src="https://wordpress.stackexchange.com/questions/67542/img/ehomls.jpg" width="185" height="72" alt="" id="realtorImg"/>
    </div>
    <div id="content">
        <?php if ( have_posts() ) : ?>

                <header class="page-header">
                    <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'custom2' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
                </header>

                <?php //WHAT IS THIS?--> twentyeleven_content_nav( 'nav-above' ); ?>

                <?php /* Start the Loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>

                    <?php
                        /* Include the Post-Format-specific template for the content.
                         * If you want to overload this in a child theme then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        /* -MY CODE- 
                        <?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
                <div class="left-column">
                    <?php the_content(); ?>
                </div>
                <?php endif; endwhile; else:endif; ?>

                <?php $i = 0; rewind_posts(); ?>

                <?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
                <div class="right-column">
                    <?php the_content(); ?>

                </div>
                <?php endif; endwhile; else:endif; ?>
                <!--<div class="pagination">-->
                    <div class="prevLink"><?php previous_posts_link('&laquo; Previous') ?></div>
                    <div class="nextLink"><?php next_posts_link('More &raquo;') ?></div>*/

                    //DEFAULT CODE
                    get_template_part( 'content', get_post_format() );
                    ?>

                <?php endwhile; ?>

                <?php //WHAT IS THIS?--> twentyeleven_content_nav( 'nav-below' ); ?>

            <?php else : ?>

                <article id="post-0" class="post no-results not-found">
                    <header class="entry-header">
                        <h1 class="entry-title"><?php _e( 'Nothing Found', 'custom2' ); ?></h1>
                    </header><!-- .entry-header -->

                    <div class="entry-content">
                        <p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'custom2' ); ?></p>
                        <?php get_search_form(); ?>
                    </div><!-- .entry-content -->
                </article><!-- #post-0 -->

            <?php endif; ?>

            </div><!-- #content -->

<?php get_footer(); ?>

1 Answer
1

Does your theme have a search.php to display the results? And if not then create one based on the codex examples or default theme or post the source to your index.php for troubleshooting.

Edit: Your search.php is difficult to debug, try following the structure of a default theme, and put something like this in your search.php to test if that might be the problem:

<?php get_header(); ?>
    <section id="primary" class="site-content">
        <div id="content" role="main">
        <?php if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; ?>
        <?php else : ?>
            <article>
                <div class="entry-content">
                    <p>Nothing found, do you want to search?</p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </article><!-- #post-0 -->
        <?php endif; ?>
        </div><!-- #content -->
    </section><!-- #primary -->
<?php get_footer(); ?>

Leave a Comment