Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //custom search
- function __search_by_title_only( $search, &$wp_query ){
- global $wpdb;
- if ( empty( $search ) )
- return $search;
- $q = $wp_query->query_vars;
- $n = ! empty( $q['exact'] ) ? '' : '%';
- $search =
- $searchand = '';
- foreach ( (array) $q['search_terms'] as $term ) {
- $term = esc_sql( $wpdb->esc_like( $term ) );
- $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
- $searchand = ' AND ';
- }
- if ( ! empty( $search ) ) {
- $search = " AND ({$search}) ";
- if ( ! is_user_logged_in() )
- $search .= " AND ($wpdb->posts.post_password = '') ";
- }
- return $search;
- }
- add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
- function ajax_search(){
- extract($_POST);
- $allsearch = new WP_Query("s=".$key."&showposts=-1&post_type=any&post_status=publish");
- if($allsearch->have_posts()): while($allsearch->have_posts()) : $allsearch->the_post();
- global $post; ?>
- <p class="cfx">
- <a href="<?php the_permalink(); ?>">
- <?php if( has_post_thumbnail() ) the_post_thumbnail('thumbnail'); ?>
- <span><?php the_title(); ?></span>
- </a>
- </p>
- <?php endwhile; else :
- echo '<mark>There were no search results. <br />Please try using more general terms to get more results.</mark>';
- endif;
- exit();
- }
- add_action('wp_ajax_ajax_search', 'ajax_search');
- add_action('wp_ajax_nopriv_ajax_search', 'ajax_search');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement