I did not find a clue how to suggested Dang in the comments . I also thought about filtering the search results using hook posts_where
, but I have no idea how to recognize if the search is being done by our widget or another search form.
I found a very simple way to filter the results that is putting an input field of type:
<input type="hidden" value="portfolio" name="post_type" />
Studying WP_Widget_Search
I see that there is no way to modify it to include the filter, so the solution is to make a copy of the widget and adapt it. The modification I did was to remove get_search_form()
and pull / modify the code for that same function .
PS: Adapt CPT to your need, here is being used portfolio
.
<?php
/**
* Plugin Name: (SOPT) Widget Search by CPT
* Plugin URI: https://pt.stackoverflow.com/a/17233/201
* Author: brasofilo
*/
add_action( 'widgets_init', 'b5f_sopt_search' );
function b5f_sopt_search() {
register_widget( 'B5F_SOPT_Search_Widget' );
}
class B5F_SOPT_Search_Widget extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your site.") );
parent::__construct( 'search', _x( 'Search', 'Search widget' ), $widget_ops );
}
function widget( $args, $instance ) {
extract($args);
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
$format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml';
if ( 'html5' == $format ) {
$form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
<label>
<span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
<input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search …', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" title="' . esc_attr_x( 'Search for:', 'label' ) . '" />
</label>
<input type="hidden" value="portfolio" name="post_type" />
<input type="submit" class="search-submit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
</form>';
} else {
$form = '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
<div>
<label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" />
<input type="hidden" value="portfolio" name="post_type" />
<input type="submit" id="searchsubmit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
</div>
</form>';
}
echo $form;
echo $after_widget;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = $instance['title'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
}
Finally, modify your code to:
echo the_widget( 'B5F_SOPT_Search_Widget' );
Related: Can not limit search to only pages
27.05.2014 / 10:36