Guest User

Untitled

a guest
Nov 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. // Search SQL filter for matching against post title only.
  2. function __search_by_title_only( $search, $wp_query )
  3. {
  4. global $wpdb;
  5.  
  6. if ( empty( $search ) )
  7. return $search; // skip processing - no search term in query
  8.  
  9. $q = $wp_query->query_vars;
  10. $n = ! empty( $q['exact'] ) ? '' : '%';
  11.  
  12. $search =
  13. $searchand = '';
  14.  
  15. foreach ( (array) $q['search_terms'] as $term ) {
  16. $term = esc_sql( $term = $wpdb->esc_like( $term ) );
  17.  
  18. $search .= "{$searchand}($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]')";
  19.  
  20. $searchand = ' AND ';
  21. }
  22.  
  23. if ( ! empty( $search ) ) {
  24. $search = " AND ({$search}) ";
  25. if ( ! is_user_logged_in() )
  26. $search .= " AND ($wpdb->posts.post_password = '') ";
  27. }
  28.  
  29. return $search;
  30. }
  31.  
  32. add_filter( 'posts_search', '__search_by_title_only', 1000, 2 );
Add Comment
Please, Sign In to add comment