in

add search results from external SQL in wordpress


Hello im looking for a solution unique to my situation. I know alot of people have asked similar questions but their solutions are not the same.

In my situation im trying to create a search listing site that pulls its listings from a external SQL DB. Similar to bestofhomecare.com

In my research ive found 2 (i think) possible solutions.

the first using the code below

function search_filter() {
    if ( is_search() ) {
        // Do your API call here
        // Save retrieved data in your wordpress
        // This will also help to you avoid api call for repeated queries.
        $post_id = wp_insert_post( $post, $wp_error ); // wp_insert_post() // Programatically insert queries result into your wordpress database
        array_push( $query->api, $post_id );
    }
}
add_action('pre_get_posts','search_filter');    
function have_posts_override(){
    if ( is_search() ) {
        global $wp_query;
        $api_post = $wp_query->api;
        foreach ($api_post as $key) {
        // This will enable you to add results you received using API call
        // into default search results.
            array_push($wp_query->posts, $key); 
        }
    }
}
add_action( 'found_posts', 'have_posts_override' );

from my understanding this will make the search results into a custom post type and display it using wordpress native search (references: Add results into WordPress search results) My only question with this route is how do i make the API call? There is a API doc i can refernce to get this data as my it director mentioned so i think this may be a option.

the other route is connecting to the db using wpdb

$mydb = new wpdb('username','password','database','localhost');
$rows = $mydb->get_results("select Name from my_table");

but im not sure how to get the data into the search results after using this line….on the stackoverflow page i referenced there is this code suggestion

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;

});

not sure if this is something i can use in tandem with the wpdb connect too get the end result im looking for but im also no entirely sure how the script above operates, if (!$sql) ???

I know i have alot of questions but i hope someone out there is willing to help me go in the right direction with this. Thank you



Source: https://stackoverflow.com/questions/70718427/add-search-results-from-external-sql-in-wordpress

A different approach to responsive styling for React Native Web apps who use Server Side Rendering

a graphical image annotation tool to generate ground plane masks for a 3D object reconstruction system