Limit search by post title only

  • icola007

    #7370

    Hi,

    I want to limit search by post title only. So par example, when I search for ‘WATCH’ and select the category ‘games’ and click search, I see ‘WATCH_DOGS’ in the list.

    Now a lot of games are displayed.

    So, I found this code and inserted it in functions.php.

    But guess what, it doesn’t work 🙁

    This is my functions.php:

    <?php
    /**
    * Graphene WordPress Theme, Copyright 2010-2012 Syahir Hakim
    * Graphene is distributed under the terms of the GNU GPL version 3
    *
    * Graphene functions and definitions
    *
    * @package Graphene
    * @since Graphene 1.0
    */

    /**
    * Before we do anything, let's get the mobile extension's init file if it exists
    */
    $mobile_path = dirname( dirname( __FILE__ ) ) . '/graphene-mobile/includes/theme-plugin.php';
    if ( file_exists( $mobile_path ) ) { include( $mobile_path ); }

    /**
    * Load the various theme files
    */
    global $graphene_settings;
    require( get_template_directory() . '/admin/options-init.php' ); // Theme options and admin interface setup
    require( $graphene_settings['template_dir'] . '/includes/theme-scripts.php' ); // Theme stylesheets and scripts
    require( $graphene_settings['template_dir'] . '/includes/theme-utils.php' ); // Theme utilities
    require( $graphene_settings['template_dir'] . '/includes/theme-head.php' ); // Functions for output into the HTML <head> element
    require( $graphene_settings['template_dir'] . '/includes/theme-menu.php' ); // Functions for navigation menus
    require( $graphene_settings['template_dir'] . '/includes/theme-loop.php' ); // Functions for posts/pages loops
    require( $graphene_settings['template_dir'] . '/includes/theme-comments.php' ); // Functions for comments
    require( $graphene_settings['template_dir'] . '/includes/theme-widgets.php' ); // Functions for custom widgets
    require( $graphene_settings['template_dir'] . '/includes/theme-slider.php' ); // Functions for the slider
    require( $graphene_settings['template_dir'] . '/includes/theme-panes.php' ); // Functions for the homepage panes
    require( $graphene_settings['template_dir'] . '/includes/theme-plugins.php' ); // Native plugins support
    require( $graphene_settings['template_dir'] . '/includes/theme-shortcodes.php' ); // Theme shortcodes
    require( $graphene_settings['template_dir'] . '/includes/theme-webfonts.php' ); // Theme webfonts
    require( $graphene_settings['template_dir'] . '/includes/theme-functions.php' ); // Other functions that are not categorised above
    require( $graphene_settings['template_dir'] . '/includes/theme-setup.php' ); // Theme setup

    function __search_by_title_only( $search, &$wp_query )
    {
    global $wpdb;
    if ( empty( $search ) )
    return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
    $term = esc_sql( like_escape( $term ) );
    $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
    $searchand = ' AND ';
    }
    if ( ! empty( $search ) ) {
    $search = " AND ({$search}) ";
    if ( ! is_user_logged_in() )
    $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
    }
    add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

    Can anyone tell me what I’ve done wrong?

    Many thanks in advance!

    Thibault Seyaneve

    P.s. I use the plugin ‘search by category’

    Mod

    Kenneth John Odle

    #37969

    Why did you include all those other functions? They are already included in the parent theme.

    You are using a child theme, right?

    icola007

    #37970

    Hi, I’m not using a child theme.

    Mod

    Kenneth John Odle

    #37971

    You really should set up a child theme for this. Otherwise, you will lose changes when the theme updates, or you can have other problems.

    icola007

    #37972

    Ok, I’m moving to another hosting company at 2 May so then I’ll setup a child theme.

    I’ll be back! 😀

    Mod

    Kenneth John Odle

    #37973

    Let us know when you get set up and if this works for you. There are some interesting bits on that website you linked to that I’ll be looking at in my spare time.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

Do NOT follow this link or you will be banned from the site!