How to display multisite search results for sidebar and search.php at the same time without slowing the search results?

Question

I have a page which searches for the whole multisite (it’s like google for my multisites). It’s working alright but it takes a while to load (depends on what term you use, it can take 2-3sec). The reason is that I’m using mostly the same code for the search.php and the sidebar.php. I’m displaying the results on search.php and I’m displaying only the title’s of the sites on sidebar.php so that I can filter them by sites. The only thing that’s different on sidebar.php is that it only displays the title as a button for filtering but it’s doing the same amount of work as search.php. When I remove the code from sidebar.php, the loading time is cut by half.

What’s the right way to do it so that the loading only happens on search.php so that it doesn’t double the time for the results to come? Here’s my code on my search.php

<div class="row">
    <div class="col-3">
        <?php get_sidebar(); ?>
    </div>

    <div class="col">

        <div class="row">
            
            <?php $query_string=esc_attr($query_string);  ?>

            <?php if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) { ?>
                <?php $blogs = get_sites(); ?>
                <?php 
                foreach ( $blogs as $blog ): 

                    switch_to_blog( $blog->blog_id );
                    $search = new WP_Query($query_string); 

                    if ( $search->found_posts>0 ) {
                        foreach ( $search->posts as $post ) {

                            setup_postdata($post);
                            $author_data = get_userdata(get_the_author_meta('ID'));
                            $current_blog_details = get_blog_details( $blog->blog_id );

                            $slug = (str_replace(' ', '-', strtolower($current_blog_details->blogname)));
                            ?>


                            <?php if ( isset($_GET['post_type'])  ) {  ?>

                                <?php $get_site = get_blog_details( $_GET['post_type'] ); ?>

                                <?php if ( $current_blog_details->blogname == $_GET['post_type'] ) : ?>
                                    <?php get_template_part( 'content', 'index' ); ?>
                                <?php endif; ?>

                            <?php } else { ?>

                                <?php get_template_part( 'content', 'index' ); ?>
                            <?php } ?>

                            <?php
                            } //foreach posts

                        } //$search->found_posts>0


                    restore_current_blog(); // Reset settings to the current blog
                endforeach;

                ?>

            <?php } //function_exists( 'get_sites' ) ?>
        </div>

    </div>
</div>

And here’s on my sidebar.php

<?php
    $searchfor = get_search_query(); // Get the search query for display in a headline
    $thisURL = get_site_url(); // Get the search query for display in a headline
    ?>

    <?php if ( isset($_GET['post_type']) )  : ?>
        <?php $posttype = $_GET['post_type']; ?>
        <?php else : ?>
            <?php $posttype = ""; ?>
        <?php endif; ?>
        <?php $items = array(); ?>



        <div id="sidebar">

            <div id="search-filter">
                <div class="d-flex flex-wrap bd-highlight">

                    <?php if ( isset($_GET['post_type']) )  : ?>
                        <div class="p-2 bd-highlight"><a href="<?php echo get_site_url()."/?s=".$searchfor; ?>" class="btn btn-secondary">All results</a></div>
                        <?php else : ?>
                            <div class="p-2 bd-highlight"><a href="<?php echo get_site_url()."/?s=".$searchfor; ?>" class="btn btn-primary">All results</a></div>
                        <?php endif; ?>

                        <?php $query_string = "s=".esc_attr($searchfor);  ?>

                        <?php if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) { ?>
                            <?php $blogs = get_sites(); ?>
                            <?php 
                            foreach ( $blogs as $blog ): 

                                switch_to_blog( $blog->blog_id );
                                $search = new WP_Query($query_string); 
                                $current = "";
                                if ( $search->found_posts>0 ) {
                                    foreach ( $search->posts as $post ) {

                                        setup_postdata($post);
                                        $author_data = get_userdata(get_the_author_meta('ID'));
                                        $current_blog_details = get_blog_details( $blog->blog_id );

                                        $slug = (str_replace(' ', '-', strtolower($current_blog_details->blogname)));
                                        ?>

                                        <?php if ( $current != $current_blog_details->blogname ) :?>
                                            

                                            <?php 
                                            $blogname = $current_blog_details->blogname; 
                                            $items[] = $search->found_posts;
                                            ?>

                                            <?php $url = $thisURL."/?s=".$searchfor."&post_type=".$blogname; ?>                         
                                            <div class="p-2 bd-highlight">          
                                                <a href="<?=$url;?>" class="btn <?php if ( $blogname == $posttype ) : echo "btn-primary"; else : echo "btn-secondary"; endif; ?>">
                                                    <?php echo $current_blog_details->blogname; ?> <small>(<?php echo $search->found_posts; ?>)</small>
                                                </a>
                                            </div>

                                            <?php 
                                            $current = $current_blog_details->blogname;
                                        else :
                                            
                                        endif;
                                        ?>

                                        <?php $arr[] = $current_blog_details->blogname; ?>
                                        <?php $unique_array = array_unique($arr) ?>
                                        
                                        <?php
                        } //foreach posts
                    } //$search->found_posts>0

                restore_current_blog(); // Reset settings to the current blog
            endforeach;

            ?>

        <?php } //function_exists( 'get_sites' ) ?>



    </div><!-- /#search-filter -->
</div><!-- /d-flex flex-wrap bd-highlight -->



</div><!-- /#sidebar -->
0
vanduzled 2 months 0 Answers 12 views 0

Leave an answer