• Resolved webston

    (@webston)


    Hello,
    this is the first post I am posting here. I searched a lot for a feature I need, but could’nt find any information.
    I have a search bar and I need to search only for post titles and tags assigned to the post.
    I tried a plugin Search everything, but it searches in content and dates also.

    I found this code, but it searches only for titles and I tried to change it to search for tags too but without luck:
    function __search_by_title_only( $search, &$wp_query )
    {
    global $wpdb;
    if ( empty( $search ) )
    return $search;
    $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 );

    Is there a plugin I can use or how can I change the code so it searches both titles and tags?

    Thank you in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search only post titles and tags’ is closed to new replies.