admin管理员组

文章数量:1336660

Im modifyng archive.php in my woocommerce theme to have multisite global search. I added this code:

    if ( is_search() ) {

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$query_args = array(
            'posts_per_page' => 48,
            'post_status'    => 'publish',
            'post_type'      => 'product',
            'orderby'        => 'ASC',
            'no_found_rows'  => 1,
            //'paged'          => -1,
        );
$query_args['product_cat'] = strip_tags( $_REQUEST['product_cat'] );  
$query_args['meta_query'][] = array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => 'NOT IN' );

do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();

$searchfor = get_search_query();
$query_string=esc_attr($query_string);
$blogs = get_sites();
foreach ( $blogs as $blog ):
switch_to_blog($blog->blog_id);
$search_query = new WP_Query($query_args);

while ( $search_query->have_posts() ) {
$search_query->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
wp_reset_postdata();
restore_current_blog();

endforeach;

  woocommerce_product_loop_end();
  do_action( 'woocommerce_after_shop_loop' );
  do_action( 'woocommerce_after_main_content' );
  do_action( 'woocommerce_sidebar' );
  get_footer( 'shop' ); 
  exit;
}
?>

Seems to work, but with some problem! For example. iIf i search a terms that has many results in site 1 and site 2 it works perfectly with pagination. If i search a term that has thousands of result in site 2 only, i can see only 36 results without pagination. Someone has any suggestion?

Im modifyng archive.php in my woocommerce theme to have multisite global search. I added this code:

    if ( is_search() ) {

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$query_args = array(
            'posts_per_page' => 48,
            'post_status'    => 'publish',
            'post_type'      => 'product',
            'orderby'        => 'ASC',
            'no_found_rows'  => 1,
            //'paged'          => -1,
        );
$query_args['product_cat'] = strip_tags( $_REQUEST['product_cat'] );  
$query_args['meta_query'][] = array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => 'NOT IN' );

do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();

$searchfor = get_search_query();
$query_string=esc_attr($query_string);
$blogs = get_sites();
foreach ( $blogs as $blog ):
switch_to_blog($blog->blog_id);
$search_query = new WP_Query($query_args);

while ( $search_query->have_posts() ) {
$search_query->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
wp_reset_postdata();
restore_current_blog();

endforeach;

  woocommerce_product_loop_end();
  do_action( 'woocommerce_after_shop_loop' );
  do_action( 'woocommerce_after_main_content' );
  do_action( 'woocommerce_sidebar' );
  get_footer( 'shop' ); 
  exit;
}
?>

Seems to work, but with some problem! For example. iIf i search a terms that has many results in site 1 and site 2 it works perfectly with pagination. If i search a term that has thousands of result in site 2 only, i can see only 36 results without pagination. Someone has any suggestion?

Share Improve this question edited May 19, 2020 at 16:41 Vito Santimone asked May 19, 2020 at 16:21 Vito SantimoneVito Santimone 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I haven't tested you method yet. However I had bookmarked few closely related topics (also for me to try & error) as below:

  1. how to override woocommerce specific loop or archive-product.php
  2. WP_Query | Help me create a search term with an 'OR' relation?
  3. Find all product that contain terms with %keywords% in WP_Query
  4. Get posts from sites in Multisite?
  5. restore_current_blog() vs switch_to_blog()
  6. How would I use pre_get_posts to query another site for posts in my multisite network?

Hope Help!

本文标签: Woocommerce Multisite Search Mod to archivephp but no pagination