WordPress.org

Ready to get started?Download WordPress

Forums

New Search Algorithm (3 posts)

  1. Sina Saeedi
    Member
    Posted 3 years ago #

    I need a little hack.
    When we search in wp, it sends the inputed sting in the name of "s" to <?php bloginfo('home'); ?> using GET method:

    <form method="get" action="<?php bloginfo('home'); ?>">
        <input type="text" name="s" id="s" size="20" />
        <input type="submit" value="<?php esc_attr_e('Search'); ?>" />

    I need to send the inputed string to <?php bloginfo('home'); ?> in the name of "x" and process my searched string with my own algorithm.

    I found some search functions in query.php and now I need to know where I must put me own search queries and how can I introduce "x" parameter to WordPress to use my own search query?

    My own search form:

    <form method="get" action="<?php bloginfo('home'); ?>">
        <input type="text" name="x" id="x" size="20" />
        <input type="submit" value="<?php esc_attr_e('Search'); ?>" />

    My search result page URL:
    http://mydomain.com/?x=string
    Instead of
    http://mydomain.com/?s=string

  2. Sina Saeedi
    Member
    Posted 3 years ago #

    I changed this in wp-includes/query.php:

    // If a search pattern is specified, load the posts that match
    		if ( !empty($q['s']) ) {
    			// added slashes screw with quote grouping when done early, so done later
    			$q['s'] = stripslashes($q['s']);
    			if ( !empty($q['sentence']) ) {
    				$q['search_terms'] = array($q['s']);
    			} else {
    				preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
    				$q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
    			}
    			$n = !empty($q['exact']) ? '' : '%';
    			$searchand = '';
    			foreach( (array) $q['search_terms'] as $term) {
    				$term = addslashes_gpc($term);
    				$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    				$searchand = ' AND ';
    			}
    			$term = esc_sql($q['s']);
    			if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
    				$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
    
    			if ( !empty($search) ) {
    				$search = " AND ({$search}) ";
    				if ( !is_user_logged_in() )
    					$search .= " AND ($wpdb->posts.post_password = '') ";
    			}
    		}

    Into this:

    // If a search pattern is specified, load the posts that match
    		if ( !empty($q['x']) ) {
    			// added slashes screw with quote grouping when done early, so done later
    			$q['x'] = stripslashes($q['x']);
    			if ( !empty($q['sentence']) ) {
    				$q['search_terms'] = array($q['x']);
    			} else {
    				preg_match_all('/".*?("|$)|((?<=[\\x",+])|^)[^\\x",+]+/', $q['x'], $matches);
    				$q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
    			}
    			$n = !empty($q['exact']) ? '' : '%';
    			$searchand = '';
    			foreach( (array) $q['search_terms'] as $term) {
    				$term = addslashes_gpc($term);
    				$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    				$searchand = ' AND ';
    			}
    			$term = esc_sql($q['x']);
    			if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['x'] )
    				$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
    
    			if ( !empty($search) ) {
    				$search = " AND ({$search}) ";
    				if ( !is_user_logged_in() )
    					$search .= " AND ($wpdb->posts.post_password = '') ";
    			}
    		}

    And I changed this in function parse_query ($query):

    elseif ( !empty($qv['s']) ) {
    			$this->is_search = true;
    		}

    Into this:

    elseif ( !empty($qv['x']) ) {
    			$this->is_search = true;
    		}

    But the x parameter still doesnt work!
    Any comment?

  3. Klark0
    Member
    Posted 3 years ago #

    I need this as well since i need to merge default WordPress search without an outside search.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags