Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?php
  2. //custom search
  3. function __search_by_title_only( $search, &$wp_query ){
  4. global $wpdb;
  5. if ( empty( $search ) )
  6. return $search;
  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 );
  24.  
  25. function ajax_search(){
  26. extract($_POST);
  27. $allsearch = new WP_Query("s=".$key."&showposts=-1&post_type=any&post_status=publish");
  28. if($allsearch->have_posts()): while($allsearch->have_posts()) : $allsearch->the_post();
  29. global $post; ?>
  30. <p class="cfx">
  31. <a href="<?php the_permalink(); ?>">
  32. <?php if( has_post_thumbnail() ) the_post_thumbnail('thumbnail'); ?>
  33. <span><?php the_title(); ?></span>
  34. </a>
  35. </p>
  36. <?php endwhile; else :
  37. echo '<mark>There were no search results. <br />Please try using more general terms to get more results.</mark>';
  38. endif;
  39. exit();
  40. }
  41. add_action('wp_ajax_ajax_search', 'ajax_search');
  42. add_action('wp_ajax_nopriv_ajax_search', 'ajax_search');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement