How to use custom search function in WordPress

I discovered this custom function in WordPress blog, and it seems to do exactly what I need. The only problem is I don’t know what to pass as the second parameter. It’s asking for a query but doesn’t the query happen INSIDE of this function? What query would I pass it?

I’ve searched for over an hour and I keep finding similar functions, so it’s my WordPress novice coming into play here.

Read More

BTW the function is supposed to accept a search term (the first parameter) and return all posts that have titles LIKE the search param.

function custom_search( $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( 'c_search', 'custom_search', 500, 2 );

Related posts

Leave a Reply

3 comments

  1. Basically the second argument in the function would be a reference. You can read about it at the following link:
    http://www.php.net/manual/en/functions.arguments.php

    In your case if you remove the second argument for the function about and define

    global $wp_query; 
    

    And then inside the function the code should still work but you will also have to change the add_filter( 'c_search', 'custom_search', 500, 1 );

    Or you can keep the function as is and define the global $wp_query; anywhere in your functions.php, if its not already defined and pass that as the parameter

  2. Actually, you shouldn’t need to pass any parameters… the key to that code it’s on the last line; what it does it’s that it adds a call to a function that will modify the data that it will receive… the arguments for add_filter are:

    1. the point where the data will be filtered
    2. the function that will receive and filter the data
    3. the priority of execution (lower is first)
    4. the number of arguments that the function defined in (2) will receive

    so those arguments are always passed on to the function

    the filter that code it’s referencing it’s probably not part of the wordpress core but of some custom plugin, framework or theme

    if you want to create a custom query for your own theme/plugin/whatever, take a look at http://codex.wordpress.org/Plugin_API/Filter_Reference/request

  3. You can also make search by following code:

    function __search_custom( $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}') OR ($wpdb->posts.post_content 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_custom', 500, 2 );
    

    if you want to search only by title then replace $search with following code:

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