The question might be old, but I had a similar problem and someone in the future maybe as well, so I'll post my (better) solution.
As said above, it would have been the cleanest solution to save the api query results in the wp_posts-table (as a custom post type maybe) and perform a normal WP-Search on them.
But in my case I had to deal with another table which cached some external data and link it to IDs of existing posts and I could not change this structure.
Therefore the Solution of Abhineet Verma (search_filter + pre_get_posts) seemed useful at first, but in the end it was not suitable for me. I was able to add additional posts to the query result, but they don't fit into the normal pagination of the results. For example, If you want to display 10 results per page and use the solution above and get twenty results from the api, you would have 30 results per page: the ten normal results, which belong to the page plus all twenty on every page. It also breaks some pagination-plugins.
So I finally solved the problem as follows. I used the filter posts_search to extend the sql-query:
add_filter('posts_search', function($sql) {
if (!$sql) {
return $sql;
}
global $wpdb;
$sqst = "select id from {$wpdb->prefix}mytable ... bla bla -> query to my custom table, wich return a bunch of IDs";
$sqlr = "AND (({$wpdb->prefix}posts.ID in ($sqst)) or (1 = 1 $sql))";
return $sqlr;
});
It looks a little bit strange but works very good. Maybe it will help someone sometimes!