Advertisement
Frunky

Ajax search

Jun 25th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. wp_localize_script( 'ajax-script', 'myAjax',
  2.     array(
  3.         'ajaxurl' => admin_url('admin-ajax.php')
  4.      )
  5. );  
  6. function gym_ajax_search() {
  7.     $query = $_POST['query'];
  8.     $args = array(
  9.         'post_type' => 'post', // тут нужный/нужные post_type's, типа wod, post, page
  10.         'post_status' => 'publish', // также можно добавить поиск по нужной категории определенного post_typ'a. https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
  11.         's' => $query
  12.     );
  13.     $loop = new WP_Query( $args );
  14.     ob_start();
  15.     if ( $loop ->have_posts() ) :
  16.         while ( $loop->have_posts() ) : $loop->the_post(); ?>
  17.         <h2><?php the_title(); ?></h2>
  18.         <div class="post-content"><?php the_content(); ?>
  19.         <?php endwhile; ?>
  20.     <?php else : ?>
  21.         <h1>Nothing found.</h1>
  22.     <?php endif;
  23.     ob_get_clean();
  24.     die();
  25. }
  26.  
  27. add_action( 'wp_ajax_gym_ajax_search', 'gym_ajax_search' );
  28. add_action( 'wp_ajax_nopriv_gym_ajax_search', 'gym_ajax_search' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement