Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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( '/' ); ?>">
      <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="../../../../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(); ?>
share|improve this question
    
I really need some help on this as soon as possible. I have added the example from codex.wordpress.org/Creating_a_Search_Page to my theme but it still goes to the index page as I stated above. Any advice? –  Mowgs Ly Oct 11 '12 at 22:41
    
For me, your above code works after changing 'name' => 'locations', to ` 'name' => 's'`. –  user33016 May 19 '13 at 9:07
add comment

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(); ?>
share|improve this answer
    
I don't have a search.php. I have searchform.php and searchpage.php. The searchpage.php is basically the exact same as the Creating a Search Page article I linked to above. I still just get sent to the home page with the tag ids added to the url. –  Mowgs Ly Oct 12 '12 at 19:33
    
In my index.php I have get_search_form() right below get_sidebar. My exact code for my searchform.php is shown above. Is the fact that I'm using wp_dropdown_categories instead of an actual form input the problem? It should still be able to read the id of the tag and form the results page, right? –  Mowgs Ly Oct 12 '12 at 20:03
    
The searchform.php is the form, searchpage.php displays the search form. You need a place that will actually display the search results. If you don't have a search.php your index.php will have to decide if it is search results and then display them, which apparently it is not doing. Please post your index.php or find an example search.php from one of the default themes included in Wordpress. –  miahelf Oct 12 '12 at 23:29
    
Ok so I really don't need the searchpage.php since I'm showing it on every page and using get_search_form() to display it. I'd rather not show the code for index.php. It's pretty much the exact same as the template index. UPDATE: even with adding the search.php nothing changed. It still does the same thing. –  Mowgs Ly Oct 13 '12 at 18:58
    
I'm confused. Does the search form follow the structure of the page.php but with the search form snippet added inside the content divs? Will post code shortly. –  Mowgs Ly Oct 15 '12 at 19:16
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.