最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - How to WP_Query() from multiple blogs and order them?

programmeradmin5浏览0评论

Dear Wordpress community,

my job seems quiet hard now. We have a blog for articles and another for videos and I now have to produce a list of it, ordered. Paginatedly. This is what I tried:

switch_to_blog(1);
$query = (new WP_Query([
    'post_type' => 'video',
    'post_status' => 'publish',
    'posts_per_page' => 30,
    'paged' => 1,
    's' => 's',
    'orderby' => 'date',
    'order' => 'DESC'
]))->posts;

and:

switch_to_blog(2);
$query = (new WP_Query([
    'post_type' => 'article',
    'post_status' => 'publish',
    'posts_per_page' => 30,
    'paged' => 1,
    's' => 's',
    'orderby' => 'date',
    'order' => 'DESC'
]))->posts;

the problem is, the output is not correct of course. Both the resultset can only be appended not merged. The best would be:

switch_to_blog([1,2]);
$query = (new WP_Query([
    'post_type' => 'article',
    'post_status' => 'publish',
    'posts_per_page' => 30,
    'paged' => 1,
    's' => 's',
    'orderby' => 'date',
    'order' => 'DESC'
]))->posts;

but as of my knowledge, its not possible. Then how?

Dear Wordpress community,

my job seems quiet hard now. We have a blog for articles and another for videos and I now have to produce a list of it, ordered. Paginatedly. This is what I tried:

switch_to_blog(1);
$query = (new WP_Query([
    'post_type' => 'video',
    'post_status' => 'publish',
    'posts_per_page' => 30,
    'paged' => 1,
    's' => 's',
    'orderby' => 'date',
    'order' => 'DESC'
]))->posts;

and:

switch_to_blog(2);
$query = (new WP_Query([
    'post_type' => 'article',
    'post_status' => 'publish',
    'posts_per_page' => 30,
    'paged' => 1,
    's' => 's',
    'orderby' => 'date',
    'order' => 'DESC'
]))->posts;

the problem is, the output is not correct of course. Both the resultset can only be appended not merged. The best would be:

switch_to_blog([1,2]);
$query = (new WP_Query([
    'post_type' => 'article',
    'post_status' => 'publish',
    'posts_per_page' => 30,
    'paged' => 1,
    's' => 's',
    'orderby' => 'date',
    'order' => 'DESC'
]))->posts;

but as of my knowledge, its not possible. Then how?

Share Improve this question asked Sep 29, 2020 at 8:24 user195354user195354 11 bronze badge 1
  • 1 I do not recommend accessing the posts variable directly, use a standard post loop instead so that lifecycle hooks can be executed. The pagination requirement also makes this significantly more difficult – Tom J Nowell Commented Sep 29, 2020 at 9:48
Add a comment  | 

1 Answer 1

Reset to default 2

You can't switch in different blogs of the network with the switch_to_blog([1,2]); function. The function needs a clearly integer value for one blog, see documentation.

WP Core

This process should help you. A custom way with the help of the WP core.

// The sites IDs
$sitesObj = get_sites([
    'site__in' => [1, 2,]
]);
// Merge
$sites = object_to_array( $sitesObj );

$args = [
    'post_type' => 'post',
    'posts_per_page' => 2,
];

// The loop
foreach ($sites as $site) {
    switch_to_blog( $site['blog_id'] );

    $loop = new WP_Query($args);

    if ( $loop->have_posts() ) {
        while ( ($loop->have_posts() ) : $loop->the_post();
            // content
        endwhile;
    }

    restore_current_blog();
}

Alternative WP Multisite Query

You can also use a custom function, solution, like the solution from Eric - https://github/ericandrewlewis/WP_Query_Multisite The solution is well documented.

The way is a little bit smaller, but you need to include this solution as a plugin or inside the Theme.

$query = new WP_Query_Multisite( array( 'post_type' => 'post' ) );

while( $query->have_posts() ) : $query->the_post();
    echo $blog_id . get_the_title() . "<br>";
endwhile;

wp_reset_postdata();
发布评论

评论列表(0)

  1. 暂无评论