rozanchetta

Wordpress - Busca pelo título do post

Sep 1st, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1. function __pesquisa_por_title( $search, &$wp_query )
  2. {
  3.     if ( empty($search) )
  4.         return $search;
  5.     $q =& $wp_query->query_vars;
  6.     // linhas do wp-includes/query.php - wp3.1
  7.     $n = !empty($q['exact']) ? '' : '%';
  8.     $searchand = '';
  9.     foreach( (array) $q['search_terms'] as $term ) {
  10.         $term = esc_sql( like_escape( $term ) );
  11.         $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
  12.         $searchand = ' AND ';
  13.     }
  14.     $term = esc_sql( like_escape( $q['s'] ) );
  15.     if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
  16.         $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
  17.     if ( !empty($search) ) {
  18.         $search = " AND ({$search}) ";
  19.         if ( !is_user_logged_in() )
  20.             $search .= " AND ($wpdb->posts.post_password = '') ";
  21.     }
  22.     return $search;
  23. }
  24. add_filter( 'posts_search', '__pesquisa_por_title', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment