I am creating an autocomplete search bar for a website using an ajax call to get a list of the people (custom post type) whose name match the query. The function works well on my local Trellis server but fails when deployed to the staging server (Trellis on Ubuntu 18.4). The install is very fresh.
I am getting a 400 error to the following page:
staging.mywebsite.com/wp/wp-admin/admin-ajax.php?action=get_name_suggestion&text=james
Here are a few snippets:
home.js
$.ajax({
url : ajax_object.ajax_url,
data : {
action: 'get_name_suggestion',
text,
},
})
.done(function (res) {
let suggestions = res.data;
update(suggestions);
});
}
helpers.php
function get_name_suggestion() {
$args = array(
'posts_per_page' => 5,
's' => esc_attr($_REQUEST['text']),
'post_type' => 'people'
);
$the_query = new \WP_Query( $args );
wp_send_json_success($the_query->posts);
}
setup.php
$ajax_params = array(
'ajax_url' => admin_url('admin-ajax.php'),
'ajax_nonce' => wp_create_nonce('my_nonce'),
);
wp_localize_script('sage/main.js', 'ajax_object', $ajax_params);