WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to change The full text search to The title search? (4 posts)

  1. maojiemng
    Member
    Posted 5 years ago #

    I found in wordpress 2.7beta that uses The full text search.
    when i search by some words,such as ONE PIECE (it's a japanse anime).

    I found there are a lot of Irrelevant search result , where artiles
    have a word PIECE or ONE .

    I hope there is an option we can select The full text search or just The Title search.

    And wordpress can search by category ?

  2. alexleonard
    Member
    Posted 5 years ago #

    I'm not sure about altering the way the search works, but I think you could implement a category search very easily.

    The default search form might be something like

    <form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
    <p><input type="text" size="14" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" /> <input type="submit" id="searchsubmit" value="<?php _e('Search'); ?>" /></p>
    </form>

    What you could do is add a select element filled with your categories so that your search form would look like

    <form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
    <p><input type="text" size="14" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
    <?php wp_dropdown_categories(); ?>
    <input type="submit" id="searchsubmit" value="<?php _e('Search'); ?>" />
    </p>
    </form>

    More info on wp_dropdown_categories

    After this you would need to modify your search.php template to GET the category that was selected and only show search results from that category using a simple query_posts.

    <p><em>You searched for "<?= $_GET['s']; ?>"</em></p>
    <ol>
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts(array('category__in' => array($_GET['cat']), 'paged' => $paged));
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li>
    <h2 class="post-title" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <p class="search-meta"><?php the_time('F jS, Y') ?></p>
    <?php the_excerpt(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>">Read more on <?php the_title(); ?></a></p>
    </li>
    <?php endwhile; ?>
    </ol>
    <?php endif; ?>

    Be warned, I haven't actually tested this out, but that's what I'd try out straight up ;)

    In fact I will be testing something like this a little later on, I'll come back here if it needs alteration!

  3. maojiemng
    Member
    Posted 5 years ago #

    I found some Code that may be usefull in wp-include/query.php
    line:1739-->line:1761

    // 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(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $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 = $wpdb->escape($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}) ";
    		}

    these code define how to search by keywords. but there don't define argument by category.

    anyone can give me some advice to change to search by category.

  4. maojiemng
    Member
    Posted 5 years ago #

    I found some Code that may be usefull in wp-include/query.php file.
    line:1739-->line:1761

    // 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(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $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 = $wpdb->escape($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}) ";
    		}

    these code define how to search by keywords. but there don't define argument by category.

    anyone can give me some advice to change to search by category.

  5. maojiemng
    Member
    Posted 5 years ago #

    I found some Code that may be usefull in wp-include/query.php file.
    line:1739-->line:1761

    // 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(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $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 = $wpdb->escape($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}) ";
    		}

    these code define how to search by keywords. but there don't define argument by category.

    anyone can give me some advice to change to search by category.

  6. maojiemng
    Member
    Posted 5 years ago #

    I found some Code that may be usefull in wp-include/query.php file.
    line:1739-->line:1761

    // 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(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $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 = $wpdb->escape($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}) ";
    }`

    these code define how to search by keywords. but there don't define argument by category.

    anyone can give me some advice to change to search by category.

  7. maojiemng
    Member
    Posted 5 years ago #

    ( 1 ) Search by Title ONLY!

    File: wp-includes/query.php

    REPLACE:

    $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}

    WITH:

    $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') )";

    REPLACE:

    $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";

    WITH:

    $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') ";

Topic Closed

This topic has been closed to new replies.

About this Topic