Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. function __search_by_title_only( $search, &$wp_query )
  2. {
  3. global $wpdb;
  4. if(empty($search)) {
  5. return $search; // skip processing - no search term in query
  6. }
  7. $q = $wp_query->query_vars;
  8. $n = !empty($q['exact']) ? '' : '%';
  9. $search =
  10. $searchand = '';
  11. foreach ((array)$q['search_terms'] as $term) {
  12. $term = esc_sql($wpdb->esc_like($term));
  13. $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
  14. $searchand = ' AND ';
  15. }
  16. if (!empty($search)) {
  17. $search = " AND ({$search}) ";
  18. if (!is_user_logged_in())
  19. $search .= " AND ($wpdb->posts.post_password = '') ";
  20. }
  21. return $search;
  22. }
  23. add_filter('posts_search', '__search_by_title_only', 500, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement