Created
November 27, 2011 13:43
-
-
Save pdewouters/1397570 to your computer and use it in GitHub Desktop.
custom search form widget
This file contains 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 | |
/* | |
Custom field search widget | |
This code adds a widget that allows to search for milepost ranges | |
*/ | |
// use widgets_init action hook to execute custom function | |
add_action( 'widgets_init', 'wpyr_milepostwidget_register_widgets' ); | |
//register our widget | |
function wpyr_milepostwidget_register_widgets() { | |
register_widget( 'wpyr_milepost_widget' ); | |
} | |
//wpyr_milepost_widget class | |
class wpyr_milepost_widget extends WP_Widget { | |
//process the new widget | |
function wpyr_milepost_widget() { | |
$widget_ops = array( | |
'classname' => 'wpyr_milepostwidget_widget_class', | |
'description' => 'Search photos by milepost.' | |
); | |
$this->WP_Widget( 'wpyr_milepost_widget', 'Milepost search Widget', $widget_ops ); | |
} | |
//build the widget settings form | |
function form($instance) { | |
$defaults = array( 'title' => 'Milepost search' ); | |
$instance = wp_parse_args( (array) $instance, $defaults ); | |
$title = $instance['title']; | |
?> | |
<p>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p> | |
<?php | |
} | |
//save the widget settings | |
function update($new_instance, $old_instance) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
return $instance; | |
} | |
//display the widget | |
function widget($args, $instance) { | |
extract($args); | |
echo $before_widget; | |
$title = apply_filters( 'widget_title', $instance['title'] ); | |
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; | |
//display search form | |
wpyr_search_form(); | |
echo $after_widget; | |
} | |
} | |
// Filters the main query on the Image CPT archive. Returns posts in between range of milepost | |
function wpyr_milepost_query( $query ) { | |
global $wp_the_query; | |
$start = $_GET['start']; | |
$end = $_GET['end']; | |
if( (isset($start) && isset($end)) && $wp_the_query === $query && !is_admin() && is_post_type_archive( 'wpyr_image' ) ) { | |
$meta_query = array( | |
array( | |
'key' => '_cmb_wpyr_milepost', | |
'value' => array( $start, $end ), | |
'type' => 'numeric', | |
'compare' => 'BETWEEN' | |
) | |
); | |
$query->set( 'meta_query', $meta_query ); | |
$query->set( 'orderby', 'menu_order' ); | |
$query->set( 'meta_key', '_cmb_wpyr_milepost' ); | |
$query->set( 'order', 'ASC' ); | |
$query->set( 'posts_per_page', '-1' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'wpyr_milepost_query' ); | |
//Builds the milepost search form | |
function wpyr_search_form(){ | |
?> | |
<form name="search" action="" method="get"> | |
<label for="start">First milepost</label> | |
<input id="start" name="start" type="text" /> | |
<label for="end">Last milepost</label> | |
<input id="end" name="end" type="text" /> | |
<input type="submit" value="search" /> | |
</form> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment