• So basically I want the search function to display results based on the title, not content. I need to know how to go about going this because all the content is auto-generated based on the custom fields entered (no text in actually body field). So when I search anything, no results display cause there “techniquely” isn’t any content on the pages. Ty in advance

Viewing 1 replies (of 1 total)
  • I think the default search does what you want already, that is, it searches for the search string in the title OR in the content. However, if that is not working for you, try adding this filter to your functions.php:

    // Alter search query to search only post title
    function mam_search_only_title( $where ) {
       global $wpdb;
       if ( is_search() ) {
          $pattern = "/\($wpdb->posts.post_content LIKE [^)]+?\)/";
          //print_r("<p>PATTERN:$pattern</p>");
          $where = preg_replace($pattern, ' ( 1 = 2 )', $where);
          //print_r("<p>WHERE:$where</p>");
       }
       return $where;
    }
    add_filter('posts_where', 'mam_search_only_title');
Viewing 1 replies (of 1 total)
  • The topic ‘Search based on title’ is closed to new replies.