好的,作为参考,我最终实际使用了switch_to_blog()
和restore_current_blog()
. 我没有找到任何用于查询多个表的引用(除了编写自己的表JOIN
-语句)执行操作的源代码时pre_get_posts
.
有人给了斯基普很多建议restore_current_blog()
为了节省CPU周期,但当我检查源代码时,我发现这会导致巨大的内存问题,因为restore_current_blog()
所做的是将最新插入的对象弹出到全局数组中,并且该数组将随着每个switch_to_blog()
那一个做不到restore_current_blog()
在…上
请随时查看源代码,并查看@user42826对此的回答,网址为https://wordpress.stackexchange.com/a/123516/14820.
对于那些感兴趣的人来说,我最终得到了站点列表和结果的瞬态,以节省CPU周期和DB性能。我只需要更新过时的数据(15分钟或更长时间)。仅包含以下相关功能位:
$site_transient = get_site_transient( \'multisite_site_list\');
if ($site_transient == false || LL_USE_TRANSIENTS == false) {
$using_site_transient = false;
// no cache available
global $wpdb;
// TODO: get prefix for wp tables
$site_list = $wpdb->get_results($wpdb->prepare(\'SELECT * FROM nw_blogs WHERE public = %d AND deleted = %d ORDER BY blog_id\', 1, 0));
// set transient
set_site_transient(\'multisite_site_list\', $site_list, LL_TRANSIENT_EXPIRES);
} else {
//log_msg("SITELIST - Transient found, using cached query..");
$site_list = $site_transient;
$using_site_transient = true;
}
//log_msg("$id - get_posts_from_network | tags: " . implode(",", $tags) . " | tag_slug__and: $lang_full");
$start = microtime(true);
// get blogpost transient, if existing
$post_transient = get_site_transient( \'limelight_recent_posts_\' . $hash);
// if not, do query and save result as a transient
if ($post_transient == false || LL_USE_TRANSIENTS == false) {
$using_post_transient = false;
$blogposts = array();
foreach ($site_list as $site) {
switch_to_blog($site->blog_id);
$posts = get_posts(array(
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'desc\',
\'tag\' => implode(",", $tags),
\'tag_slug__and\' => array($lang_full)
));
//log_msg("$id - get_posts_form_network | at site: $site->blog_id");
foreach ($posts as $post) {
//log_msg("$id - get_posts_form_network | at site: $site->blog_id | got post!");
$post->human_post_date = human_time_diff(get_post_time(\'U\', false, $post->ID), current_time(\'timestamp\')) . \' \' . __(\'ago\', \'limelight\');
if (isset($tagline_metabox)) {
$meta = get_post_meta($post->ID, $tagline_metabox->get_the_id(), TRUE);
$post->tagline = $meta[\'tagline_text\'];
}
if (!isset($post->tagline) || $post->tagline == \'\')
$post->tagline = substr($post->post_title, 0, 20) . \'..\';
$post->post_date_timestamp = strtotime($post->post_date);
//$post->blog_id = $site->blog_id;
$post->blog_name = get_bloginfo(\'name\');
$post->blog_url = get_bloginfo(\'wpurl\');
$post->permalink = get_permalink($post->ID);
$post->author_name = get_userdata($post->post_author)->display_name;
//$data = get_userdata($post->post_author);
$blogposts[] = $post;
}
restore_current_blog();
}
// sort blogposts by post_date_timestamp, descending
array_sort_by_obj($blogposts, \'post_date_timestamp\', SORT_DESC);
// pick five latest posts
if (sizeof($blogposts) > 5) {
$posts = array();
for ($i = 0; $i <= 4; $i++) {
$posts[$i] = $blogposts[$i];
}
$blogposts = $posts;
}
// set transient
if (LL_USE_TRANSIENTS)
set_site_transient(\'limelight_recent_posts_\' . $hash, $blogposts, LL_TRANSIENT_EXPIRES);