'search-post-type', 'description' => 'A search widget that will only search a selected post type' ); /* Widget control settings. */ $control_ops = array( 'width' => 350, 'height' => 350, 'id_base' => 'search-post-type' ); /* Create the widget. */ $this->WP_Widget( 'search-post-type', 'Search Post Type', $widget_ops, $control_ops ); // my code 1 add_filter('pre_get_posts', array($this,'mySearchFilter')); } // begin my code 2 function mySearchFilter($query) { $post_type = $_GET['post_type']; if (!$post_type) { $post_type = 'any'; } if ($query->is_search) { $query->set('post_type', $post_type); }; return $query; } // end mycode 2 /** @see WP_Widget::widget */ function widget($args, $instance) { extract( $args ); /* Our variables from the widget settings */ $title = apply_filters('widget_title', $instance['title'] ); $text = $instance['text']; $button = $instance['button']; $type = $instance['type']; echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; ?>
__('', 'search-post-type'), 'text' => __('Search', 'search-post-type'), 'button' => __('Submit', 'search-post-type'), 'type' => __('', 'search-post-type') ); $instance = wp_parse_args( (array) $instance, $defaults ); ?>