В jQuery при отправке формы у меня есть
Код: Выделить всё
$.ajax({
type: "POST",
async: false,
url: location.protocol + '//' + window.location.hostname + '/wp-json/w/v2/search_community',
data: {
searchTerm: $('.open-search-mobile .community-search input[name=communitysearch]').val(),
},
success: function (response) {
$('.search-body .search-results').empty();
$('.search-body .search-results').append(response);
},
error: function (xhr) {
console.log("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
Код: Выделить всё
add_action('rest_api_init', function () {
register_rest_route('w/v2', 'search_community', [
'methods' => 'POST',
'callback' => 'search_community',
]);
});
function search_community(){
global $post;
$searchterm = $_POST['searchTerm'];
$topic_args = [
'post_type' => 'topic',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 25,
's' => $searchterm,
];
$topics = new WP_Query($topic_args);
...................
$post_args = [
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'posts_per_page' => 25,
's' => $searchterm,
];
$posts = new WP_Query($post_args);
.......................
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... h-rest-api
Мобильная версия