query_vars; $n = ! empty( $q['exact'] ) ? '' : '%'; $search = "$wpdb->posts.post_type = '".esc_attr($_GET['posttype'])."' AND "; $searchand = ''; foreach( (array) $q['search_terms'] as $term ) { $term = esc_sql( like_escape( $term ) ); //push search "OR's" $list = array(); if (isset($searchtitle) && $searchtitle == 1) array_push($list,"($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')"); if (isset($searchcontent) && $searchcontent == 1) array_push($list,"($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')"); if (isset($searchtags) && $searchtags == 1) array_push($list,"(t.name like '{$n}{$term}{$n}' AND post_status = 'publish' and tt.taxonomy in ('post_tag', 'category'))"); $search .= "{$searchand}"; $search .= "( "; $search .= implode(" OR ",$list); $search .= ")"; $searchand = ' AND '; } if ( ! empty( $search ) ) { $search = " AND ({$search}) "; if ( ! is_user_logged_in() ) $search .= " AND ($wpdb->posts.post_password = '') "; } } return $search; } add_filter('posts_where','advancedSearchWidget_searchquery'); function advancedSearchWidget_searchjoin($join) { if (is_search()) { if (isset($_GET['widget'])) { extract(advancedSearchWidget_getvars($_GET['widget'])); if (isset($searchtags) && $searchtags == 1) { global $table_prefix, $wpdb; $tabletags = $table_prefix . "terms"; $tablepost2tag = $table_prefix . "term_relationships"; $tabletaxonomy = $table_prefix . "term_taxonomy"; $join .= " LEFT JOIN $tablepost2tag tr ON $wpdb->posts.ID = tr.object_id INNER JOIN $tabletaxonomy tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN $tabletags t ON t.term_id = tt.term_id "; } } } return $join; } add_filter('posts_join','advancedSearchWidget_searchjoin'); function advancedSearchWidget_searchgroupby($groupby) { if (is_search()) { if (isset($_GET['widget'])) { extract(advancedSearchWidget_getvars($_GET['widget'])); if (isset($searchtags) && $searchtags == 1) { global $wpdb; // we need to group on post ID $mygroupby = "{$wpdb->posts}.ID"; if( preg_match( "/$mygroupby/", $groupby )) { // grouping we need is already there return $groupby; } if( !strlen(trim($groupby))) { // groupby was empty, use ours return $mygroupby; } // wasn't empty, append ours return $groupby . ", " . $mygroupby; } } } return $groupby; } add_filter('posts_groupby', 'advancedSearchWidget_searchgroupby'); function customsearchwidget_getquery($id) { $widget = esc_attr($_GET['widget']); $posttype = esc_attr($_GET['posttype']); if (!empty($widget) && !empty($posttype)) { if ($widget == $id) return $_GET['s']; } } #function postsrequeststmp($sql) { # if (is_search()) { # print_r($sql); # } # return $sql; #} #add_filter('posts_request','postsrequeststmp'); /** * Adds Advanced_Search widget. */ class Advanced_Search_Widget extends WP_Widget { /** * Register widget with WordPress. */ public function __construct() { parent::__construct( 'advanced-search-widget', // Base ID 'Advanced Search Widget', // Name array( 'description' => __( 'Advanced search widget', 'advanced-search-widget' ), ) // Args ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; $form = '
'; echo $form; echo $after_widget; } /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = strip_tags( $new_instance['title'] ); $instance['posttype'] = strip_tags( $new_instance['posttype'] ); $instance['searchtitle'] = (empty($new_instance['searchtitle']) ? '0' : strip_tags( $new_instance['searchtitle'] )); $instance['searchcontent'] = (empty($new_instance['searchcontent']) ? '0' : strip_tags( $new_instance['searchcontent'] )); $instance['searchtags'] = (empty($new_instance['searchtags']) ? '0' : strip_tags( $new_instance['searchtags'] )); return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'New title', 'advanced-search-widget' ); } if (isset($instance['searchtitle'])) { $searchtitle = ($instance['searchtitle'] == 1 ? ' checked="checked"' : ''); } else { $searchtitle = ' checked="checked"'; } if (isset($instance['searchcontent'])) { $searchcontent = ($instance['searchcontent'] == 1 ? ' checked="checked"' : ''); } else { $searchcontent = ' checked="checked"'; } if (isset($instance['searchtags'])) { $searchtags = ($instance['searchtags'] == 1 ? ' checked="checked"' : ''); } else { $searchtags = ''; } $custom_post_types = get_post_types( array('exclude_from_search' => false) ); #array_unshift($custom_post_types,'any'); ?>
type="checkbox" value="1" />
type="checkbox" value="1" />
type="checkbox" value="1" />