0

My theme's search.php currently has a few "IF" statements to deal with the different types of search that my website uses.

I have a site-wide search, a species (custom post type) specific search and an advanced species search.

For the "species" search, I've simply added an input akin to <input type="hidden" name="type" value="species" />. This search uses a single input field to search my species custom post type. Users would likely opt to use this search if they were looking for the common or scientific name of a fish.

For the "advanced species" search, I'm using 12 different inputs (pH, size, temp, etc). I'm building an $args array for meta_query in the WP_Query class with this data, i.e:

array( 'key' => 'genus', 'value' => $_GET["genus"] )

As a result however, the generic <input name="s"> isn't being used, and as such, the form action isn't working (just loads the index page without searching).

Is there a better way of doing this? I could obviously just send "s" as a hidden input with a generic value, but that's a cheap and ugly way of going about things.

Maybe this should be a page separate to our search page? I'd prefer to do it in search.php though or there will be some redundant copying of HTML structure etc for no real reason.

Thanks in advance,

2
  • 1
    If you're worried about C+Ping HTML structure, why not use template parts to render the results? That way you can separate out the explicit metaquery logic into its own, more relevant template. Commented Jan 12, 2012 at 14:28
  • This is something I'd never struck on and might be quite handy. Thanks Simon, I'll investigate :)
    – turbonerd
    Commented Jan 12, 2012 at 14:35

2 Answers 2

0

If you use something like as you suggest why is that cheap and ugly? it saves you from having to rebuild a whole search mechanism, and dealing with the template hierarchy. and as you say it's sharing html on the search page. Go for it!

1
  • Yeah of course you're right, maybe it's just a bit of an OCD thing - I thoroughly dislike the idea of "cheating" WordPress behaviour :) If all else fails, maybe this is the answer!
    – turbonerd
    Commented Jan 12, 2012 at 14:34
0

I ended up taking @Simon Scarfe 's advice using get_template_part.

Just in case anyone ever needs it, this page (a work in progress) creates an advanced search form, then builds a WP_Query to search a custom post type:

<?php if (isset($_POST["act"]) && $_POST["act"] == "s") : ?>

<?php
    $meta_query = array('relation' => 'AND');

    if (!empty($_POST["s_genus"])) {
        $genus = array(
            'key' => 'genus',
            'value' => $_POST["s_genus"],
            'compare' => 'LIKE'
        );
        $meta_query[] = $genus;
    }

    if (!empty($_POST["s_species"])) {
        $species = array(
            'key' => 'species',
            'value' => $_POST["s_species"],
            'compare' => 'LIKE'
        );
        $meta_query[] = $species;
    }

    $args = array(
        'post_type' => 'species',
        'meta_query' => $meta_query
    );

    get_template_part('searchresults');
?>

<?php else : ?>

    <div id="content">
        <h2>ADVANCED<span class="lblue">SEARCH</span></h2>

        <form action="" method="post">
            <input type="hidden" name="act" value="s" />
            <fieldset id="taxonomy" style="margin-right: 15px;">
                <legend>Taxonomy</legend>
                <label for="genus">Genus</label>
                    <input class="full" type="text" name="s_genus" />
                <br />
                <label for="species">Species</label>
                    <input class="full" type="text" name="s_species" />
                <br />
                <label for="family">Family</label>
                    <input class="full" type="text" name="s_family" />
            </fieldset>
        </form>
    </div>
<?php endif; ?>

Then, in searchresults.php I have this code, which is just a couple of lines then a single change to a standard page template:

<?php global $args; ?>

<?php $query = new WP_Query ( $args ); ?>

<?php if ($query->have_posts()) : ?>
    <div class="post" id="post-<?php the_ID(); ?>">

    <p class="info">Search results for <em>&#8216;<?php echo $s ?>&#8217;</em></p>

    </div>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

            <small><?php the_time('F jS, Y') ?> &mdash; <?php the_time('h:i a') ?> <?php edit_post_link('Edit','<strong> |</strong> ',''); ?> </small>

            <div class="entry">
                <?php the_content('Continue reading &raquo;'); ?>
            </div>

            <p class="info"><?php comments_popup_link('Comment &raquo;', '1 comment &raquo;', '% comments &raquo;'); ?> <strong>|</strong> <?php the_category(', ') ?></p>
        </div>
    <?php endwhile; ?>
    <p align="center"><?php next_posts_link('&laquo; Previous Entries') ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php previous_posts_link('Next Entries &raquo;') ?></p><?php else : ?>

    <h2>Not Found</h2>

    <p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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