• I would like to edit my search functions to only show search results for my posts titles. I had my theme designed for me so things are a bit different than a typical theme. My search.php file looks like this:

    <? get_header()?>
            <div id="sub_header">
                <?php include (TEMPLATEPATH . '/includes/search_form.php'); ?>
            </div>
            <div id="content">
    
                <div id="left_2_col">
    
                    <div id="search_results">
                        <h2 class="search_results">Search Results</h2>
                        <div id="search_results_top"></div>
    
                        <h3>Search Results for "<?= get_search_query() ?>". <!-- <span class="showing">Showing 0 - 0 of 0.</span>--></h3>
    
                        <? if (have_posts()) : ?>
                            <table>
                                <tr>
                                    <th>Title</th>
                                    <th>Post Date</th>
                                    <th>Category</th>
                                </tr>
                            <? $i = 0; while (have_posts()) : the_post(); $i++; ?>
                                <tr <?=((($i % 2) == 1) ? "class=\"alt\"" : NULL)?>>
                                    <td>
                                        <a href="<? the_permalink() ?>" rel="bookmark" title="Permanent Link to <? the_title_attribute() ?>"><? the_title() ?></a>
                                    </td>
                                    <td>
                                        <? the_time('F j, Y') ?>
                                    </td>
                                    <td>
                                        <? if (get_post_meta($post->ID, 'mf_page_type', TRUE) == "Blog") echo "Blog"; ?>
                                        <? if (get_post_meta($post->ID, 'mf_page_type', TRUE) == "News") echo "News"; ?>
                                    </td>
                                </tr>
                            <? endwhile; ?>
    
                            </table>

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try this on for size:

    <? get_header()?>
            <div id="sub_header">
                <?php include (TEMPLATEPATH . '/includes/search_form.php'); ?>
            </div>
            <div id="content">
    
                <div id="left_2_col">
    
                    <div id="search_results">
                        <h2 class="search_results">Search Results</h2>
                        <div id="search_results_top"></div>
    
                        <h3>Search Results for "<?= get_search_query() ?>". <!-- <span class="showing">Showing 0 - 0 of 0.</span>--></h3>
    
                        <? if (have_posts()) : ?>
                            <table>
                                <tr>
                                    <th>Title</th>
                                </tr>
                            <? $i = 0; while (have_posts()) : the_post(); $i++; ?>
                                <tr <?=((($i % 2) == 1) ? "class=\"alt\"" : NULL)?>>
                                    <td>
                                        <a href="<? the_permalink() ?>" rel="bookmark" title="Permanent Link to <? the_title_attribute() ?>"><? the_title() ?></a>
                                    </td>
                                </tr>
                            <? endwhile; ?>
    
                            </table>
    Thread Starter coryarobertson

    (@coryarobertson)

    That edit will only remove the date and category columns displayed in my search results. What I would like to do is have my search queries only search through my post titles. For example, if I searched for “wordpress” my return results would only include posts that have “wordpress” in the title as opposed to every post that has “wordpress” in it’s content.

    I hope that makes sense. I don’t think I made that clear in my initial post. Thank you for your reply.

    Cory, what you need then isn’t to edit your theme’s files, but to have a custom search plugin written.

    You can exclude post content with a filter/action like so…

    <?php
    /*
    Plugin Name: Don't search post content
    */
    add_action( 'posts_search', 'dont_search_post_content', 10000, 1 );
    function dont_search_post_content( $search_sql ) {
    	global $wpdb;
    
    	if( strpos( $search_sql, 'post_content LIKE' ) )
    		$search_sql = preg_replace( "|OR \({$wpdb->posts}.post_content LIKE '(.+)'\)|", '', $search_sql );
    
    	return $search_sql;
    }
    ?>

    Should work with the code currently in WordPress 3.0.1 and 3.1 … 😉

    Install:
    Create a file, anything you like, plonk the code above into that file, and save it with PHP extension (or rename the file so it has a PHP extension). Upload to plugins folder, activate it in the same way you would any other plugin..

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude content from search results’ is closed to new replies.