From ef0a7f0bedc2ddb4d5cfe14d995ec6414f5eb500 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 19:33:12 -0300 Subject: [PATCH 01/11] new version --- README | 25 --------- index.php | 154 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 80 insertions(+), 99 deletions(-) delete mode 100644 README diff --git a/README b/README deleted file mode 100644 index 020453c..0000000 --- a/README +++ /dev/null @@ -1,25 +0,0 @@ -WP_Query_Multisite is a subclass of WP_Query, WordPress' post querying class. The class does everything behind the scenes, so the only change you make to query multisite is the in the class declaration expression. - -Example usage: - -$query = new WP_Query_Multisite( array('post_type' => 'post' ) ); -while($query->have_posts()) : $query->the_post(); - echo $blog_id . get_the_title() . "
"; -endwhile; -wp_reset_postdata(); - -To modify what sites are queried, create a 'sites' element in the $args in the constructor parameter, with a sub-element of either 'sites__in' or 'sites__not_in', which will be an array similar to 'posts__in' in the WP_Query object. - -EX: - -$args = array( - 'post_type' => 'post', - 'sites' => array( - 'sites__in' => array( 1, 2, 3, 5) - ) -); -$query = new WP_Query_Multisite( $args ); -while($query->have_posts()) : $query->the_post(); - echo $blog_id . get_the_title() . "
"; -endwhile; -wp_reset_postdata(); \ No newline at end of file diff --git a/index.php b/index.php index 328d192..283ac35 100644 --- a/index.php +++ b/index.php @@ -1,100 +1,106 @@ args = $args; - $this->parse_multisite_args(); - $this->add_filters(); - $this->query($args); - $this->remove_filters(); + function __construct() { + add_filter('query_vars', array($this, 'query_vars')); + add_action('pre_get_posts', array($this, 'pre_get_posts'), 100); + add_filter('posts_request', array($this, 'create_and_unionize_select_statements'), 10, 2); + add_filter('posts_fields', array($this, 'add_site_ID_to_posts_fields'), 10, 2); + add_action('the_post', array($this, 'switch_to_blog_while_in_loop')); + } + function query_vars($vars) { + $vars[] = 'multisite'; + $vars[] = 'sites__not_in'; + $vars[] = 'sites__in'; + $vars[] = 'sites_to_query'; + return $vars; } - function parse_multisite_args() { - global $wpdb; - - $site_IDs = $wpdb->get_col( "select blog_id from $wpdb->blogs" ); - - if ( isset( $this->args['sites']['sites__not_in'] ) ) - foreach($site_IDs as $key => $site_ID ) - if (in_array($site_ID, $this->args['sites']['sites__not_in']) ) unset($site_IDs[$key]); - - if ( isset( $this->args['sites']['sites__in'] ) ) - foreach($site_IDs as $key => $site_ID ) - if ( ! in_array($site_ID, $this->args['sites']['sites__in']) ) - unset($site_IDs[$key]); - - - $site_IDs = array_values($site_IDs); - $this->sites_to_query = $site_IDs; - } + function pre_get_posts($query) { + if($query->get('multisite')) { - function add_filters() { - - add_filter('posts_request', array(&$this, 'create_and_unionize_select_statements') ); - add_filter('posts_fields', array(&$this, 'add_site_ID_to_posts_fields') ); - add_action('the_post', array(&$this, 'switch_to_blog_while_in_loop')); + global $wpdb; - } - function remove_filters() { - remove_filter('posts_request', array(&$this, 'create_and_unionize_select_statements') ); - remove_filter('posts_fields', array(&$this, 'add_site_ID_to_posts_fields') ); + $site_IDs = $wpdb->get_col( "select blog_id from $wpdb->blogs" ); - } + if ( $query->get('sites__not_in') ) + foreach($site_IDs as $key => $site_ID ) + if (in_array($site_ID, $query->get('sites__not_in')) ) unset($site_IDs[$key]); + + if ( $query->get('sites__in') ) + foreach($site_IDs as $key => $site_ID ) + if ( ! in_array($site_ID, $query->get('sites__in')) ) + unset($site_IDs[$key]); - function create_and_unionize_select_statements($sql) { - global $wpdb; + $site_IDs = array_values($site_IDs); - $root_site_db_prefix = $wpdb->prefix; - - $page = $this->args['paged'] ? $this->args['paged'] : 1; - $posts_per_page = $this->args['posts_per_page'] ? $this->args['posts_per_page'] : 10; - - foreach ($this->sites_to_query as $key => $site_ID) : + $query->set('sites_to_query', $site_IDs); + } + } - switch_to_blog($site_ID); + function create_and_unionize_select_statements($sql, $query) { + if($query->get('multisite')) { + global $wpdb; - $new_sql_select = str_replace($root_site_db_prefix, $wpdb->prefix, $sql); - $new_sql_select = preg_replace("/ LIMIT ([0-9]+), ".$posts_per_page."/", "", $new_sql_select); - $new_sql_select = str_replace("SQL_CALC_FOUND_ROWS ", "", $new_sql_select); - $new_sql_select = str_replace("# AS site_ID", "'$site_ID' AS site_ID", $new_sql_select); - $new_sql_select = preg_replace( '/ORDER BY ([A-Za-z0-9_.]+)/', "", $new_sql_select); - $new_sql_select = str_replace(array("DESC", "ASC"), "", $new_sql_select); + $root_site_db_prefix = $wpdb->prefix; - $new_sql_selects[] = $new_sql_select; - restore_current_blog(); + $page = $query->get('paged') ? $query->get('paged') : 1; + $posts_per_page = $query->get('posts_per_page') ? $query->get('posts_per_page') : 10; + + $sites_to_query = $query->get('sites_to_query'); + + foreach($sites_to_query as $key => $site_ID) { - endforeach; + switch_to_blog($site_ID); - if ( $posts_per_page > 0 ) { - $skip = ( $page * $posts_per_page ) - $posts_per_page; - $limit = "LIMIT $skip, $posts_per_page"; - } else { - $limit = ''; - } - $orderby = "tables.post_date DESC"; - $new_sql = "SELECT SQL_CALC_FOUND_ROWS tables.* FROM ( " . implode(" UNION ", $new_sql_selects) . ") tables ORDER BY $orderby " . $limit; + $new_sql_select = str_replace($root_site_db_prefix, $wpdb->prefix, $sql); + $new_sql_select = preg_replace("/ LIMIT ([0-9]+), ".$posts_per_page."/", "", $new_sql_select); + $new_sql_select = str_replace("SQL_CALC_FOUND_ROWS ", "", $new_sql_select); + $new_sql_select = str_replace("# AS site_ID", "'$site_ID' AS site_ID", $new_sql_select); + $new_sql_select = preg_replace( '/ORDER BY ([A-Za-z0-9_.]+)/', "", $new_sql_select); + $new_sql_select = str_replace(array("DESC", "ASC"), "", $new_sql_select); + + $new_sql_selects[] = $new_sql_select; + restore_current_blog(); - return $new_sql; + } + + if ( $posts_per_page > 0 ) { + $skip = ( $page * $posts_per_page ) - $posts_per_page; + $limit = "LIMIT $skip, $posts_per_page"; + } else { + $limit = ''; + } + $orderby = "tables.post_date DESC"; + $sql = "SELECT SQL_CALC_FOUND_ROWS tables.* FROM ( " . implode(" UNION ", $new_sql_selects) . ") tables ORDER BY $orderby " . $limit; + + } + return $sql; } - function add_site_ID_to_posts_fields($sql) { - $sql_statements[] = $sql; - $sql_statements[] = "# AS site_ID"; - return implode(', ', $sql_statements); + function add_site_ID_to_posts_fields($sql, $query) { + if($query->get('multisite')) { + $sql_statements[] = $sql; + $sql_statements[] = "# AS site_ID"; + $sql = implode(', ', $sql_statements); + } + return $sql; } function switch_to_blog_while_in_loop($post) { - global $blog_id; - if($post->site_ID && $blog_id != $post->site_ID ) - switch_to_blog($post->site_ID); - else - restore_current_blog(); + global $wp_query; + if($wp_query->get('multisite')) { + global $blog_id; + if($post->site_ID && $blog_id != $post->site_ID ) + switch_to_blog($post->site_ID); + else + restore_current_blog(); + } } } +new WP_Query_Multisite(); + ?> From 550c0b5943ea56d5325f686bef4d4f1b176ad1db Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 19:33:58 -0300 Subject: [PATCH 02/11] update readme --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..b795a11 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# WordPress Query Multisite + +WP_Query_Multisite is a subclass of WP_Query, WordPress' post querying class. The class does everything behind the scenes, so the only change you make to query multisite is the in the class declaration expression. + +---------------- + +This is a custom version to support multisite queries without the need of calling a subclass. Just by entering the custom query var **multisite => 1** on your query args your query is now global. + +----------------- + +## Example usage + +```php +$query = new WP_Query( array('multisite' => '1' ) ); +while($query->have_posts()) : $query->the_post(); + echo $blog_id . get_the_title() . "
"; +endwhile; +wp_reset_postdata(); +``` + +To modify what sites are queried, create a 'sites' element in the $args in the constructor parameter, with a sub-element of either 'sites__in' or 'sites__not_in', which will be an array similar to 'posts__in' in the WP_Query object. + +```php +$args = array( + 'multisite' => 1, + 'sites' => array( + 'sites__in' => array( 1, 2, 3, 5) + ) +); +$query = new WP_Query( $args ); +while($query->have_posts()) : $query->the_post(); + echo $blog_id . get_the_title() . "
"; +endwhile; +wp_reset_postdata +``` \ No newline at end of file From 279e033412eff2d2f12da68930755293e3c68b07 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 19:45:53 -0300 Subject: [PATCH 03/11] more examples --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b795a11..887f8fa 100644 --- a/README.md +++ b/README.md @@ -31,5 +31,20 @@ $query = new WP_Query( $args ); while($query->have_posts()) : $query->the_post(); echo $blog_id . get_the_title() . "
"; endwhile; -wp_reset_postdata +wp_reset_postdata(); +``` + +### Automatic multisite search example + +On your functions.php: + +```php +include_once(TEMPLATEPATH . '/path/to/multisite-query.php'); + +function my_multisite_search($query) { + if(!is_admin() && $query->is_main_query() && $query->is_search) { + $query->set('multisite', 1); + } +} +add_action('pre_get_posts', 'my_multisite_search'); ``` \ No newline at end of file From 0010535a47c1fe337d5fca0fa9fe56aa290a6d80 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 21:27:32 -0300 Subject: [PATCH 04/11] fixes --- index.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index 283ac35..0a38cec 100644 --- a/index.php +++ b/index.php @@ -8,6 +8,7 @@ function __construct() { add_filter('posts_request', array($this, 'create_and_unionize_select_statements'), 10, 2); add_filter('posts_fields', array($this, 'add_site_ID_to_posts_fields'), 10, 2); add_action('the_post', array($this, 'switch_to_blog_while_in_loop')); + add_action('loop_end', array($this, 'loop_end')); } function query_vars($vars) { @@ -22,6 +23,9 @@ function pre_get_posts($query) { if($query->get('multisite')) { global $wpdb; + + $this->loop_end = false; + $this->blog_id = $blog_id; $site_IDs = $wpdb->get_col( "select blog_id from $wpdb->blogs" ); @@ -90,13 +94,19 @@ function add_site_ID_to_posts_fields($sql, $query) { } function switch_to_blog_while_in_loop($post) { - global $wp_query; - if($wp_query->get('multisite')) { - global $blog_id; - if($post->site_ID && $blog_id != $post->site_ID ) - switch_to_blog($post->site_ID); - else - restore_current_blog(); + global $blog_id; + if(!$this->loop_end && $post->site_ID && $blog_id !== $post->site_ID) { + switch_to_blog($post->site_ID); + } + } + + function loop_end($query) { + global $switched; + if($query->get('multisite')) { + $this->loop_end = true; + if($switched) { + switch_to_blog($this->blog_id); + } } } } From 0beae18201d944f7b13e1b9fcdf4b8b2a5d72f7b Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 21:49:36 -0300 Subject: [PATCH 05/11] more fixes --- index.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index 0a38cec..9338388 100644 --- a/index.php +++ b/index.php @@ -5,9 +5,9 @@ class WP_Query_Multisite { function __construct() { add_filter('query_vars', array($this, 'query_vars')); add_action('pre_get_posts', array($this, 'pre_get_posts'), 100); - add_filter('posts_request', array($this, 'create_and_unionize_select_statements'), 10, 2); - add_filter('posts_fields', array($this, 'add_site_ID_to_posts_fields'), 10, 2); - add_action('the_post', array($this, 'switch_to_blog_while_in_loop')); + add_filter('posts_request', array($this, 'posts_request'), 10, 2); + add_filter('posts_fields', array($this, 'posts_fields'), 10, 2); + add_action('the_post', array($this, 'the_post')); add_action('loop_end', array($this, 'loop_end')); } @@ -22,7 +22,7 @@ function query_vars($vars) { function pre_get_posts($query) { if($query->get('multisite')) { - global $wpdb; + global $wpdb, $blog_id; $this->loop_end = false; $this->blog_id = $blog_id; @@ -44,7 +44,7 @@ function pre_get_posts($query) { } } - function create_and_unionize_select_statements($sql, $query) { + function posts_request($sql, $query) { if($query->get('multisite')) { global $wpdb; @@ -84,7 +84,7 @@ function create_and_unionize_select_statements($sql, $query) { return $sql; } - function add_site_ID_to_posts_fields($sql, $query) { + function posts_fields($sql, $query) { if($query->get('multisite')) { $sql_statements[] = $sql; $sql_statements[] = "# AS site_ID"; @@ -93,7 +93,7 @@ function add_site_ID_to_posts_fields($sql, $query) { return $sql; } - function switch_to_blog_while_in_loop($post) { + function the_post($post) { global $blog_id; if(!$this->loop_end && $post->site_ID && $blog_id !== $post->site_ID) { switch_to_blog($post->site_ID); From 637fa95ad075aa5502de3a3058e881d0ea25f42b Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 22:25:07 -0300 Subject: [PATCH 06/11] update readme --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index 887f8fa..841c005 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,6 @@ # WordPress Query Multisite -WP_Query_Multisite is a subclass of WP_Query, WordPress' post querying class. The class does everything behind the scenes, so the only change you make to query multisite is the in the class declaration expression. - ----------------- - -This is a custom version to support multisite queries without the need of calling a subclass. Just by entering the custom query var **multisite => 1** on your query args your query is now global. +This is a custom version of [WP_Query_Multisite](https://github.com/ericandrewlewis/WP_Query_Multisite), by [ericandrewlewis](https://github.com/ericandrewlewis), to support multisite queries without changing the class declaration expression. Just by entering the custom query var **multisite => 1** on your query args your query is now global. ----------------- From ea3da353d275c1645bc53e8988007d5fbb05c7ce Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 22:26:37 -0300 Subject: [PATCH 07/11] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 841c005..bca1836 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # WordPress Query Multisite -This is a custom version of [WP_Query_Multisite](https://github.com/ericandrewlewis/WP_Query_Multisite), by [ericandrewlewis](https://github.com/ericandrewlewis), to support multisite queries without changing the class declaration expression. Just by entering the custom query var **multisite => 1** on your query args your query is now global. +This is a custom version of [WP_Query_Multisite](https://github.com/ericandrewlewis/WP_Query_Multisite), by [ericandrewlewis](https://github.com/ericandrewlewis), to support multisite post queries but without changing the class declaration, we will use the good ol' **WP_Query**. Just by entering the custom query var **multisite => 1** on your query args and your query is now global. ----------------- From aa23b5d707193d83012315edd629b47caa713cee Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Mon, 2 Sep 2013 22:59:10 -0300 Subject: [PATCH 08/11] fix example --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index bca1836..ca32ba0 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,7 @@ To modify what sites are queried, create a 'sites' element in the $args in the c ```php $args = array( 'multisite' => 1, - 'sites' => array( - 'sites__in' => array( 1, 2, 3, 5) - ) + 'sites__in' => array( 1, 2, 3, 5) ); $query = new WP_Query( $args ); while($query->have_posts()) : $query->the_post(); From b1025f0c6b67ddf48724137c2d693b6c2afb4824 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 3 Sep 2013 00:13:38 -0300 Subject: [PATCH 09/11] change ms query to support custom order and other features --- index.php | 70 +++++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/index.php b/index.php index 9338388..22bafd2 100644 --- a/index.php +++ b/index.php @@ -1,12 +1,12 @@ loop_end = false; $this->blog_id = $blog_id; - + $site_IDs = $wpdb->get_col( "select blog_id from $wpdb->blogs" ); if ( $query->get('sites__not_in') ) @@ -40,64 +39,65 @@ function pre_get_posts($query) { $site_IDs = array_values($site_IDs); - $query->set('sites_to_query', $site_IDs); + $this->sites_to_query = $site_IDs; } } - function posts_request($sql, $query) { + function posts_clauses($clauses, $query) { if($query->get('multisite')) { global $wpdb; - $root_site_db_prefix = $wpdb->prefix; - - $page = $query->get('paged') ? $query->get('paged') : 1; - $posts_per_page = $query->get('posts_per_page') ? $query->get('posts_per_page') : 10; + // Orderby for tables (not wp_posts) + $clauses['orderby'] = str_replace($wpdb->posts, 'tables', $clauses['orderby']); - $sites_to_query = $query->get('sites_to_query'); + // State new selection to replace wp_posts on posts_request + $this->ms_select = array(); - foreach($sites_to_query as $key => $site_ID) { + $root_site_db_prefix = $wpdb->prefix; + foreach($this->sites_to_query as $site_ID) { switch_to_blog($site_ID); - $new_sql_select = str_replace($root_site_db_prefix, $wpdb->prefix, $sql); - $new_sql_select = preg_replace("/ LIMIT ([0-9]+), ".$posts_per_page."/", "", $new_sql_select); - $new_sql_select = str_replace("SQL_CALC_FOUND_ROWS ", "", $new_sql_select); - $new_sql_select = str_replace("# AS site_ID", "'$site_ID' AS site_ID", $new_sql_select); - $new_sql_select = preg_replace( '/ORDER BY ([A-Za-z0-9_.]+)/', "", $new_sql_select); - $new_sql_select = str_replace(array("DESC", "ASC"), "", $new_sql_select); - - $new_sql_selects[] = $new_sql_select; + $ms_select = str_replace($root_site_db_prefix, $wpdb->prefix, $clauses['where']); + $ms_select = " SELECT $wpdb->posts.*, '$site_ID' as site_ID FROM $wpdb->posts WHERE 1=1 $ms_select "; + + $this->ms_select[] = $ms_select; + restore_current_blog(); } - if ( $posts_per_page > 0 ) { - $skip = ( $page * $posts_per_page ) - $posts_per_page; - $limit = "LIMIT $skip, $posts_per_page"; - } else { - $limit = ''; - } - $orderby = "tables.post_date DESC"; - $sql = "SELECT SQL_CALC_FOUND_ROWS tables.* FROM ( " . implode(" UNION ", $new_sql_selects) . ") tables ORDER BY $orderby " . $limit; + // Clear where to populate on posts_request; + $clauses['where'] = ''; } - return $sql; + return $clauses; } - - function posts_fields($sql, $query) { + + function posts_request($sql, $query) { + if($query->get('multisite')) { - $sql_statements[] = $sql; - $sql_statements[] = "# AS site_ID"; - $sql = implode(', ', $sql_statements); + + global $wpdb; + + // Clean up remanescent WHERE request + $sql = str_replace('WHERE 1=1', '', $sql); + + // Multisite request + $sql = str_replace("$wpdb->posts.* FROM $wpdb->posts", 'tables.* FROM ( ' . implode(" UNION ", $this->ms_select) . ' ) tables', $sql); + } + return $sql; } function the_post($post) { global $blog_id; + if(!$this->loop_end && $post->site_ID && $blog_id !== $post->site_ID) { switch_to_blog($post->site_ID); } + } function loop_end($query) { @@ -113,4 +113,4 @@ function loop_end($query) { new WP_Query_Multisite(); -?> +?> \ No newline at end of file From 325ee59161ec388b385653d252de1937d34e5a79 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Tue, 3 Sep 2013 00:55:15 -0300 Subject: [PATCH 10/11] some query fixes --- index.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index 22bafd2..364993a 100644 --- a/index.php +++ b/index.php @@ -47,10 +47,7 @@ function posts_clauses($clauses, $query) { if($query->get('multisite')) { global $wpdb; - // Orderby for tables (not wp_posts) - $clauses['orderby'] = str_replace($wpdb->posts, 'tables', $clauses['orderby']); - - // State new selection to replace wp_posts on posts_request + // Start new mysql selection to replace wp_posts on posts_request hook $this->ms_select = array(); $root_site_db_prefix = $wpdb->prefix; @@ -58,8 +55,13 @@ function posts_clauses($clauses, $query) { switch_to_blog($site_ID); - $ms_select = str_replace($root_site_db_prefix, $wpdb->prefix, $clauses['where']); - $ms_select = " SELECT $wpdb->posts.*, '$site_ID' as site_ID FROM $wpdb->posts WHERE 1=1 $ms_select "; + $ms_select = $clauses['join'] . ' WHERE 1=1 '. $clauses['where']; + + if($clauses['groupby']) + $ms_select .= ' GROUP BY ' . $clauses['groupby']; + + $ms_select = str_replace($root_site_db_prefix, $wpdb->prefix, $ms_select); + $ms_select = " SELECT $wpdb->posts.*, '$site_ID' as site_ID FROM $wpdb->posts $ms_select "; $this->ms_select[] = $ms_select; @@ -67,8 +69,13 @@ function posts_clauses($clauses, $query) { } - // Clear where to populate on posts_request; + // Clear join, where and groupby to populate with parsed ms select on posts_request hook; + $clauses['join'] = ''; $clauses['where'] = ''; + $clauses['groupby'] = ''; + + // Orderby for tables (not wp_posts) + $clauses['orderby'] = str_replace($wpdb->posts, 'tables', $clauses['orderby']); } return $clauses; From bab280e14bd73bcae477bd37909efabbd7307c33 Mon Sep 17 00:00:00 2001 From: Ephraim Gregor Date: Mon, 18 Jan 2016 11:55:17 -0500 Subject: [PATCH 11/11] Fixes notice error. --- index.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.php b/index.php index 364993a..2369319 100644 --- a/index.php +++ b/index.php @@ -17,7 +17,7 @@ function query_vars($vars) { $vars[] = 'sites__in'; return $vars; } - + function pre_get_posts($query) { if($query->get('multisite')) { @@ -31,10 +31,10 @@ function pre_get_posts($query) { if ( $query->get('sites__not_in') ) foreach($site_IDs as $key => $site_ID ) if (in_array($site_ID, $query->get('sites__not_in')) ) unset($site_IDs[$key]); - + if ( $query->get('sites__in') ) foreach($site_IDs as $key => $site_ID ) - if ( ! in_array($site_ID, $query->get('sites__in')) ) + if ( ! in_array($site_ID, $query->get('sites__in')) ) unset($site_IDs[$key]); $site_IDs = array_values($site_IDs); @@ -97,11 +97,11 @@ function posts_request($sql, $query) { return $sql; } - + function the_post($post) { global $blog_id; - if(!$this->loop_end && $post->site_ID && $blog_id !== $post->site_ID) { + if( isset( $this->loop_end ) && !$this->loop_end && $post->site_ID && $blog_id !== $post->site_ID) { switch_to_blog($post->site_ID); } @@ -120,4 +120,4 @@ function loop_end($query) { new WP_Query_Multisite(); -?> \ No newline at end of file +?>