admin管理员组文章数量:1130349
So I have a Primary site that posts announcements about the sub sites, for each of those posts I have a meta value that the user sets for which site those announcements apply too. On each of those sub sites, I'd like to include those announcements for that site in the regular post loop.
So far I've only been able to display them separately
$latest_posts = get_posts( array(
'numberposts' => 5
) );
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 5,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
Tried merging the networkPosts query with the regular query using Pre_Get_Posts, but it was a mess.
function add_network_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$id = get_current_blog_id();
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 0,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
restore_current_blog();
$query = array_merge($query, $networkPosts );
}
}
add_action( 'pre_get_posts', 'add_network_to_query' );
TLDR; I want to include the posts from Site 1 with a certain meta value, into my query for each individual sub site.
So I have a Primary site that posts announcements about the sub sites, for each of those posts I have a meta value that the user sets for which site those announcements apply too. On each of those sub sites, I'd like to include those announcements for that site in the regular post loop.
So far I've only been able to display them separately
$latest_posts = get_posts( array(
'numberposts' => 5
) );
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 5,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
Tried merging the networkPosts query with the regular query using Pre_Get_Posts, but it was a mess.
function add_network_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$id = get_current_blog_id();
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 0,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
restore_current_blog();
$query = array_merge($query, $networkPosts );
}
}
add_action( 'pre_get_posts', 'add_network_to_query' );
TLDR; I want to include the posts from Site 1 with a certain meta value, into my query for each individual sub site.
Share Improve this question asked Aug 20, 2018 at 19:58 Picard102Picard102 9331 gold badge10 silver badges23 bronze badges1 Answer
Reset to default -1If you want to work with it just like with WP_Query, you can use this construction.
$query = new Network_Query( array(
'blog_id' => array( 1, get_current_blog_id() ),
'posts_per_page' => 5,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
) );
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
endwhile;
endif;
So, we merged the current blog and the blog with ID1 into a single loop.
To make the Network_Query work on your website, additional plugin is required https://rudrastyh/plugins/get-posts-from-all-blogs-in-multisite-network
Hope it helps.
So I have a Primary site that posts announcements about the sub sites, for each of those posts I have a meta value that the user sets for which site those announcements apply too. On each of those sub sites, I'd like to include those announcements for that site in the regular post loop.
So far I've only been able to display them separately
$latest_posts = get_posts( array(
'numberposts' => 5
) );
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 5,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
Tried merging the networkPosts query with the regular query using Pre_Get_Posts, but it was a mess.
function add_network_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$id = get_current_blog_id();
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 0,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
restore_current_blog();
$query = array_merge($query, $networkPosts );
}
}
add_action( 'pre_get_posts', 'add_network_to_query' );
TLDR; I want to include the posts from Site 1 with a certain meta value, into my query for each individual sub site.
So I have a Primary site that posts announcements about the sub sites, for each of those posts I have a meta value that the user sets for which site those announcements apply too. On each of those sub sites, I'd like to include those announcements for that site in the regular post loop.
So far I've only been able to display them separately
$latest_posts = get_posts( array(
'numberposts' => 5
) );
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 5,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
Tried merging the networkPosts query with the regular query using Pre_Get_Posts, but it was a mess.
function add_network_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$id = get_current_blog_id();
switch_to_blog('1');
$networkPosts = get_posts( array(
'posts_per_page' => 0,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
));
restore_current_blog();
$query = array_merge($query, $networkPosts );
}
}
add_action( 'pre_get_posts', 'add_network_to_query' );
TLDR; I want to include the posts from Site 1 with a certain meta value, into my query for each individual sub site.
Share Improve this question asked Aug 20, 2018 at 19:58 Picard102Picard102 9331 gold badge10 silver badges23 bronze badges1 Answer
Reset to default -1If you want to work with it just like with WP_Query, you can use this construction.
$query = new Network_Query( array(
'blog_id' => array( 1, get_current_blog_id() ),
'posts_per_page' => 5,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-1130 days'))
),
'meta_query' => array(
array(
'key' => 'shows',
'value' => $id,
'compare' => 'LIKE',
)
)
) );
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
endwhile;
endif;
So, we merged the current blog and the blog with ID1 into a single loop.
To make the Network_Query work on your website, additional plugin is required https://rudrastyh/plugins/get-posts-from-all-blogs-in-multisite-network
Hope it helps.
本文标签: multisiteInclude Site 1 Posts in Query for Sub Sites
版权声明:本文标题:multisite - Include Site 1 Posts in Query for Sub Sites 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749103900a2316434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论