Skip to content

Instantly share code, notes, and snippets.

Created October 8, 2014 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/def4306301f3a5e1ae52 to your computer and use it in GitHub Desktop.
Save anonymous/def4306301f3a5e1ae52 to your computer and use it in GitHub Desktop.
Postcode Search
<?php
/* Template Name: SearchWP Supplemental Search Engine Postcode Search */
global $post;
// retrieve our search query if applicable
$query = isset( $_REQUEST['postcode'] ) ? sanitize_text_field( $_REQUEST['postcode'] ) : '';
?>
<form action="/" method="get" class="postcode-search m2-m3 t2-t5 d3-d8">
<h1>Book your clean</h1>
<div class="input-group">
<input type="text" placeholder="Enter the first part of your postcode" name="postcode" value="<?php echo esc_attr( $query ); ?>" required pattern="[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}" />
<input type="submit" id="search" value="<?php _e( 'Find a Cleaner', 'domestique2014' ); ?>" class="button" />
</div><!-- END .input-group -->
</form><!-- END .postcode-search -->
<!-- TODO: Add code comments -->
<?php if( !empty( $query ) && class_exists( 'SearchWP' ) ) : ?>
<?php
// instantiate SearchWP
$engine = SearchWP::instance();
$supplementalSearchEngineName = 'postcode_finder';
// perform the search
$posts = $engine->search( $supplementalSearchEngineName, $query );
?>
<?php if( !empty( $posts ) ) : ?>
<?php // the loop ?>
<?php foreach ( $posts as $post ): setup_postdata( $post ); ?>
<?php // redirect to the post (postcode)
wp_redirect( get_permalink( $post->ID ) ); exit;
?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
Redirect to a "no branch" page <!-- TODO: Add redirect no branch page -->
<?php endif; ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment