Search code examples
phpjquerywordpresssearchwordpress-rest-api

Searching through multiple Wordpress site post


We have multiple Worpdress blog sites (20+ sites) with each of them on their own domain and have now started creating a main website were we would like to add a search function / bar, where a visitor can do a search on the main site and it will then return a result of all the post that matches their search. From there on if they click on a post that was listed it will take them to the post on the relevant blog site, so no need to have a 2nd page for the post on the main site.

My thought so far was to use the WordPress Rest API and "wp_remote_get" function to accomplish this but there is some concerns regarding the speed of the search when the number of blog sites increase and security.

Moving all the blogs to a single site or multi site setup is not a option because I would like the option the be able to move the sites to other host if needed.

So I was wondering if anyone maybe knows of a better way do approach project.


Solution

  • Firstly, I'd try to handle a query on backend. Then obtain search results for each blog and finally Echo them to the page content.

    Also you need to find a php file (in your active theme) that shows search results. Usually it is something like search.php or content-search.php.

    <?php
    /**
     * The template for displaying search results pages
     *
    ...
    
    $query=get_search_query();
    
    $args = array( 's' => $query );
    
    $the_query = new WP_Query( $args );
    
    $posts = $the_query->posts;
    
    foreach($posts as $post) {
        // Echo some info
        // echo $post->post_name . '<br>';
    }
    

    This will show results for the current blog. So, you need to switch to every blog and get its posts for the same query ($query=get_search_query()).

    https://developer.wordpress.org/reference/functions/get_sites/

    https://developer.wordpress.org/reference/functions/switch_to_blog/

    $sites = get_sites();
        foreach ( $sites as $site ) {
            switch_to_blog( $site->blog_id );
    
                    $query=get_search_query();
        
                    $args = array( 's' => $query );
        
                    $the_query = new WP_Query( $args );
        
                    $posts = $the_query->posts;
        
                    foreach($posts as $post) {
                       // Echo some info
                      // echo $post->post_name . '<br>';
                     }
            restore_current_blog();
        }
    

    UPD: yeap, this works for wp multysite only. Here is the option for separate sites

    When you search something in your site, basically, you make a GET request:

    https://your-wp-site.com/?s=QUERY_STRING&submit=Search
    

    So, you can make Ajax GET requests ?s=QUERY_STRING&submit=Search for each domain, using js. Then parse results and insert them to the content, also using js.

    function loadDoc(url) {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
    
         //parsing this.responseText
    
        var main_content = document.getElementById("main"); /*supposing your main content tag has id='main'*/
    
        main_content.appendChild(PARSED_ELEMENT_WITH_RESULTS);
    
        }
      };
      xhttp.open("GET", url, true);
      xhttp.send();
    }
    
    
    
    var query = window.location.href.split('/')[3]; /*getting query string ?s=QUERY_STRING&submit=Search */
    
    loadDoc("https://site1.com/"+query );
    
    loadDoc("https://site2.com/"+query );
    
    loadDoc("https://site3.com/"+query );
    
    ...
    

    Finally, add this javascript file to the search page.