-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp_query_multisite.php
104 lines (74 loc) · 2.91 KB
/
wp_query_multisite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
class WP_Query_Multisite extends WP_Query{
var $args;
function __construct( $args = array() ) {
$this->args = $args;
$this->parse_multisite_args();
$this->add_filters();
$this->query($args);
$this->remove_filters();
}
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 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'));
add_action('loop_end', array(&$this, 'restore_current_blog_after_loop'));
}
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') );
}
function create_and_unionize_select_statements($sql) {
global $wpdb;
$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) :
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;
restore_current_blog();
endforeach;
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;
return $new_sql;
}
function add_site_ID_to_posts_fields($sql) {
$sql_statements[] = $sql;
$sql_statements[] = "# AS site_ID";
return implode(', ', $sql_statements);
}
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);
}
function restore_current_blog_after_loop() {
restore_current_blog();
}
}
?>