Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

renzoster on "HELP Search filters"

$
0
0

Hi,
Please I have a some problem with filters in search in WordPress, it's for a music website and I need to filter the search by artist (taxonomy), album (title, thats ok), songs (meta) and regular search. I only have album and regular, but I have some problems with artist and songs. Please help me.

class Search_Filters{
	function __construct(){
		add_filter( 'query_vars', array($this,'register_vars') );
		$this->search_hooks();
	}

	function register_vars($vars){
		$vars[] = "type";
		return $vars;
	}

	function search_hooks(){

		if ( get_query_var('type')=='artista' ) {
			add_filter( 'posts_join', array( &$this, 'search_by_artista_join' ), 10, 2);
			add_filter( 'posts_where', array( &$this, 'search_by_artista_where' ), 10, 2);
		}

		add_filter( 'posts_search', array( &$this, 'search_where' ), 10, 2 );

		add_filter( 'posts_where', array( &$this, 'no_revisions' ) );
		add_filter( 'posts_where', array( &$this, 'no_future' ) );
	}

	function search_where($where, $wp_query){
		if ( !$wp_query->is_search() )
			return $where;

		$this->query_instance = &$wp_query;
		global $wpdb;

		$searchQuery = $this->search_default();

		if( get_query_var('type')=='album' ){
			$searchQuery = $this->search_by_title();
		}
		if( get_query_var('type')=='artista' ){
			$searchQuery = $this->search_by_artista_where();
		}

		if ( $searchQuery != '' ) {
			$where = preg_replace( '#\(\(\(.*?\)\)\)#', '(('.$searchQuery.'))', $where );
		}

		return $where;
	}

	function search_default(){ 

		global $wpdb;

		$not_exact = empty($this->query_instance->query_vars['exact']);
		$search_sql_query = '';
		$seperator = '';
		$terms = $this->get_search_terms();

		// if it's not a sentance add other terms
		$search_sql_query .= '(';
		foreach ( $terms as $term ) {
			$search_sql_query .= $seperator;

			$esc_term = esc_sql($term);
			if ($not_exact) {
				$esc_term = "%$esc_term%";
			}

			$like_title = "($wpdb->posts.post_title LIKE '$esc_term')";
			$like_post = "($wpdb->posts.post_content LIKE '$esc_term')";

			$search_sql_query .= "($like_title OR $like_post)";

			$seperator = ' AND ';
		}

		$search_sql_query .= ')';
		return $search_sql_query;
	}

	function search_by_title(){ 

		global $wpdb;

		$not_exact = empty($this->query_instance->query_vars['exact']);
		$search_sql_query = '';
		$seperator = '';
		$terms = $this->get_search_terms();

		// if it's not a sentance add other terms
		$search_sql_query .= '(';
		foreach ( $terms as $term ) {
			$search_sql_query .= $seperator;

			$esc_term = esc_sql($term);
			if ($not_exact) {
				$esc_term = "%$esc_term%";
			}

			$like_title = "($wpdb->posts.post_title LIKE '$esc_term')";

			$search_sql_query .= "($like_title)";

			$seperator = ' AND ';
		}

		$search_sql_query .= ')';
		return $search_sql_query;
	}

	function search_by_artista(){ 

		global $wpdb;

		$not_exact = empty($this->query_instance->query_vars['exact']);
		$search_sql_query = '';
		$seperator = '';
		$terms = $this->get_search_terms();

		// if it's not a sentance add other terms
		$search_sql_query .= '(';
		foreach ( $terms as $term ) {
			$search_sql_query .= $seperator;

			$esc_term = esc_sql($term);
			if ($not_exact) {
				$esc_term = "%$esc_term%";
			}

			$like_title = "($wpdb->posts.post_title LIKE '$esc_term')";
			$like_post = "($wpdb->posts.post_content NOT LIKE '$esc_term')";

			$search_sql_query .= "($like_title OR $like_post)";

			$seperator = ' AND ';
		}

		$search_sql_query .= ')';
		return $search_sql_query;
	}

	function search_by_artista_join( $join ) {
		global $wpdb;

		if ( !empty( $this->query_instance->query_vars['s'] ) ) {
			//	join term_relationships, term_taxonomy, and terms into the current SQL where clause
			$join .= "
			INNER JOIN
			  {$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id
			INNER JOIN
			  {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
			INNER JOIN
			  {$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id ";

		}

		return $join;
	}
/*
	function search_by_artista_join( $join ) {
		global $wpdb;

		if ( !empty( $this->query_instance->query_vars['s'] ) ) {

			// if we're searching for categories
			if ( $this->options['se_use_category_search'] ) {
				$on[] = "ttax.taxonomy = 'artista'";
			}

			$on = ' ( ' . implode( ' OR ', $on ) . ' ) ';

			$join .= " LEFT JOIN $wpdb->term_relationships AS trel ON ($wpdb->posts.ID = trel.object_id) LEFT JOIN $wpdb->term_taxonomy AS ttax ON ( " . $on . " AND trel.term_taxonomy_id = ttax.term_taxonomy_id) LEFT JOIN $wpdb->terms AS tter ON (ttax.term_id = tter.term_id) ";
		}
		//$this->se_log( "tags join: ".$join );
		return $join;
	}
*/
	function search_by_artista_where( $where, $query ){
		global $wpdb;
		if ( !empty( $this->query_instance->query_vars['s'] ) ) {
			$where .= " OR ( {$wpdb->term_taxonomy}.taxonomy IN('artista') ";
 			$where .= " AND {$wpdb->terms}.name LIKE '%" . $wpdb->escape( get_query_var('s') ) . "%' )";
		}
		return $where;
	}

	function get_search_terms() {
		global $wpdb;
		$s = isset( $this->query_instance->query_vars['s'] ) ? $this->query_instance->query_vars['s'] : '';
		$sentence = isset( $this->query_instance->query_vars['sentence'] ) ? $this->query_instance->query_vars['sentence'] : false;
		$search_terms = array();

		if ( !empty( $s ) ) {
			// added slashes screw with quote grouping when done early, so done later
			$s = stripslashes( $s );
			if ( $sentence ) {
				$search_terms = array( $s );
			} else {
				preg_match_all( '/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches );
				$search_terms = array_map( create_function( '$a', 'return trim($a, "\\"\'\\n\\r ");' ), $matches[0] );
			}
		}
		return $search_terms;
	}

	// FIXES
	function no_revisions( $where ) {
		global $wpdb;
		if ( !empty( $this->query_instance->query_vars['s'] ) ) {
			if ( !$this->wp_ver28 ) {
				$where = 'AND (' . substr( $where, strpos( $where, 'AND' )+3 ) . ") AND $wpdb->posts.post_type != 'revision'";
			}
			$where = ' AND (' . substr( $where, strpos( $where, 'AND' )+3 ) . ') AND post_type != \'revision\'';
		}
		return $where;
	}

	function no_future( $where ) {
		global $wpdb;
		if ( !empty( $this->query_instance->query_vars['s'] ) ) {
			if ( !$this->wp_ver28 ) {
				$where = 'AND (' . substr( $where, strpos( $where, 'AND' )+3 ) . ") AND $wpdb->posts.post_status != 'future'";
			}
			$where = 'AND (' . substr( $where, strpos( $where, 'AND' )+3 ) . ') AND post_status != \'future\'';
		}
		return $where;
	}

}

Viewing all articles
Browse latest Browse all 8245

Trending Articles