Wordpress search function with ajax

admin

Administrator
Staff member
I'm trying to use the wordpress form to show the results with ajax and append into a div, here's my code:

Code:
jQuery( document ).ready(function ( $ ) {
    $( "#searchform" ).on( "submit", function ( ev ) {
        ev.preventDefault();

        $.post(
            "<?php echo admin_url( 'admin-ajax.php' ) ?>",
            {
                action: "wpa56343_search",
                search: $( "#s" ).val()
            },
            function ( response ) {
                $( ".search-content" ).html( response );
            }
        );
    });
});



And this is in my functions.php

Code:
function wpa56343_search() {
  if ( ! isset( $_POST['search'] ) )
      exit;

  query_posts(
      array(
          'posts_per_page' => 5,
          'no_found_rows' => true,
          'post_type' => get_post_types( array( 'public' => true ) ),
          's' => wp_unslash( ( string ) $_POST['search'] ),
      )
  );

}

add_action( 'wp_ajax_nopriv_wpa56343_search', 'wpa56343_search', 100 );
add_action( 'wp_ajax_wpa56343_search',        'wpa56343_search', 100 );

What am I missing? thank you.