WordPress search function with ajax

问题内容:

I’m trying to use the wordpress form to show the results with ajax and append into a div, here’s my 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

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.

问题评论:

答案:

答案1:

Question answered with this link

答案评论:

原文地址:

https://stackoverflow.com/questions/47755198/wordpress-search-function-with-ajax

添加评论

友情链接:蝴蝶教程