admin管理员组文章数量:1122846
I'm trying to integrate a custom search function in my WordPress site where an external API is used to determine search results. The function is supposed to retrieve a list of post IDs from the API based on the search query, and then display those posts as search results.
Here's the core part of my function:
function custom_search_query($query) {
if (!is_admin() && $query->is_main_query() && $query->is_search()) {
$search_query = get_search_query(); // Retrieve the search query
$api_url = '';
$args = array(
'body' => json_encode(array('query' => $search_query)),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 60
);
$response = wp_remote_post($api_url, $args);
$body = wp_remote_retrieve_body($response);
$results = json_decode($body, true);
if (isset($results['ids']) && is_array($results['ids'])) {
$query->set('post__in', $results['ids']);
$query->set('orderby', 'post__in');
$query->set('posts_per_page', -1);
}
}
}
add_action('pre_get_posts', 'custom_search_query');
However, no posts are returned when using the search, despite confirming that the API does return valid IDs. Could anyone help identify what might be going wrong here? Is there something I'm missing in setting up the query parameters or handling the API response?
Any suggestions or insights would be greatly appreciated!
I'm trying to integrate a custom search function in my WordPress site where an external API is used to determine search results. The function is supposed to retrieve a list of post IDs from the API based on the search query, and then display those posts as search results.
Here's the core part of my function:
function custom_search_query($query) {
if (!is_admin() && $query->is_main_query() && $query->is_search()) {
$search_query = get_search_query(); // Retrieve the search query
$api_url = 'https://api.adenwalla.in/api/search/data';
$args = array(
'body' => json_encode(array('query' => $search_query)),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 60
);
$response = wp_remote_post($api_url, $args);
$body = wp_remote_retrieve_body($response);
$results = json_decode($body, true);
if (isset($results['ids']) && is_array($results['ids'])) {
$query->set('post__in', $results['ids']);
$query->set('orderby', 'post__in');
$query->set('posts_per_page', -1);
}
}
}
add_action('pre_get_posts', 'custom_search_query');
However, no posts are returned when using the search, despite confirming that the API does return valid IDs. Could anyone help identify what might be going wrong here? Is there something I'm missing in setting up the query parameters or handling the API response?
Any suggestions or insights would be greatly appreciated!
Share Improve this question asked Jul 2, 2024 at 19:34 Juned AdenwallaJuned Adenwalla 213 bronze badges 1- Hi, Could you please share some of the post titles for which API returning the result. I have tried with few titles but it is not working. – Narendra Sishodiya Commented Jul 12, 2024 at 10:11
1 Answer
Reset to default 0Here's the right code I made a mistake with s param
function custom_search_query($query) {
if (!is_admin() && $query->is_main_query() && $query->is_search()) {
$search_query = get_search_query(); // Retrieve the search query
$api_url = 'https://api.adenwalla.in/api/search/data';
$args = array(
'body' => json_encode(array('query' => $search_query)),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 60
);
$response = wp_remote_post($api_url, $args);
$body = wp_remote_retrieve_body($response);
$results = json_decode($body, true);
if (isset($results['ids']) && is_array($results['ids'])) {
$query->set('post__in', $results['ids']);
$query->set('orderby', 'post__in');
$query->set('posts_per_page', -1);
$query->set('s', '');
}
}
}
add_action('pre_get_posts', 'custom_search_query');
本文标签: pluginsCustom Search Function in WordPress Returns No Results
版权声明:本文标题:plugins - Custom Search Function in WordPress Returns No Results 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300538a1930851.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论