Support » Plugin: WP Extended Search » Cannot get it to work with ajax call

  • Hi Great Plugin,

    I am using your plugin and I am moving my site over to a completely ajax setup and your plugin doesn’t seem to take effect in my ajax calls.

    I am doing the follows

     $.ajax({
                    type: "post",
                    dataType: "json",
                    url: ajax_url,
                    data: {
                        action: 'search_api_post',
                        search: 'tag'
                    }
                    success: function(response) {
    
                        console.log(response);
    
                    }
                });

    And php call

    function search_api_post() {
    
        $s = sanitize_text_field($_REQUEST['search']);
        $loop = new WP_Query(array(
            'posts_per_page'   => -1,
            's' => $s
        )); 
    
        // Setup empty array
        $dataPosts = [];
        if ( $loop->have_posts() ) { 
    
            while ( $loop->have_posts() ) : $loop->the_post(); 
    
                $dataPosts[] = array(
                    'title' => get_the_title()
                );
    
            endwhile;
            wp_reset_query();
    
            echo json_encode(
                array(
                    'error' => false,
                    'data' => $dataPosts,
                    'message' => 'Succesfully returning results' 
                )
            );
    
        }else{
    
            echo json_encode(
                array(
                    'error' => true,
                    'message' => 'No results found' 
                )
            );
    
        }  
    
        die(); 
    
    }
    
    add_action( "wp_ajax_search_api_post", "search_api_post" );
    add_action( "wp_ajax_nopriv_search_api_post", "search_api_post" );

    Any suggests?

    Thanks

    • This topic was modified 2 years, 3 months ago by melindasmith.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Cannot get it to work with ajax call’ is closed to new replies.