Created
October 8, 2014 11:28
-
-
Save anonymous/def4306301f3a5e1ae52 to your computer and use it in GitHub Desktop.
Postcode Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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