AJAX search query for custom post type in wordpress

78
July 23, 2018, at 3:50 PM

I am developing a search field in wordpress using AJAX.I am done with the query. But it takes around 1 and half minute to search and return the value. I just want to figure out a better way to do this. Please help me.

What Happens here is : I have a custom post type named reviewplans and it has a field named pin (added using acf). I want to search the field value pin and match it with the user entered pin. The current code is working.It returns the values to the console. But it takes much time.How can I reduce it or Is there any other way to approach this issue?

JQuery

    $('#review-submit').on('click',function(){
        var $secret_pin= $('#pin').val();
        jQuery.ajax({
              type : 'post',
              url : ajax_review_plan_object.ajaxurl,
              data : {
                  action : 'loadReviewPlansByAjax',
                  'pin': $secret_pin
              },
              contentType: "application/x-www-form-urlencoded; charset=UTF-8",
              success: function(response){
                console.log(response);
              }
            });
      });

PHP

function loadReviewPlansByAjax(){
  $pin= $_POST['pin'];
  $args = array(
      'post_type' => 'reviewplans',
      'post_status' => 'publish'
  );
  $reviewplans = new WP_Query( $args );
   ob_start();
  if( $reviewplans->have_posts() ) {
    if($pin == ''){
      echo 1;
    }else{
        while( $reviewplans->have_posts() ) {
          echo get_field('pin');
        }
    }
  }else{
    echo -1;
  }
}
add_action( 'wp_ajax_loadReviewPlansByAjax', 'loadReviewPlansByAjax' );
add_action( 'wp_ajax_nopriv_loadReviewPlansByAjax', 'loadReviewPlansByAjax' 
);
function ajax_coming_soon_enqueues() {
    wp_enqueue_script( 'review-plans-ajax-script', get_stylesheet_directory_uri() . '/framework/assets/js/review-plan-ajax.js', array( 'jquery' ), '1.0.0', true );
    wp_localize_script( 'review-plans-ajax-script', 'ajax_review_plan_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'ajax_coming_soon_enqueues' );
READ ALSO
Handle Codeception's WebDriver Exceptions globaly

Handle Codeception's WebDriver Exceptions globaly

I'm looking for a clean way how to handle WebDriver's exceptions in CodeceptionSome exceptions are catched and reported to test results (e

32
How to create a new session after deleting the previous one?

How to create a new session after deleting the previous one?

I'm trying to setup a session after complete destroy sessions, here is my code:

55
How to output characters 195 and 192 into HTML UTF8?

How to output characters 195 and 192 into HTML UTF8?

I want to echo a crude directory structureUsing the ascii characters 192 and 195

88
quickbooks desktop odbc connection string stuck in apache port

quickbooks desktop odbc connection string stuck in apache port

Good Day Guys i'm stuck in apache port every 5th connection in quickbooks desktop

61