0

Currently this is my WordPress Multisite:

Main site

-Sub Site

-Sub Site

-Sub Site

-Sub Site

and this is the example code for getting all post from multisite :

foreach( $blog_ids as $id ) {

    switch_to_blog( $id['blog_id'] );

    $args = array('post_type' => 'events', 'posts_per_page' => 2,'paged' => $paged,
      $meta_query = 
        array(
          'relation' => 'OR',
          array(
            'key' => 'event_date',
            'value' => date('Ymd'),
            'type' => 'DATE',
            'compare' => '>='
          ),
          array(
            'key' => 'event_second_date',
            'value' => date('Ymd'),
            'type' => 'DATE',
            'compare' => '>='
          ),
        ),
        'orderby' => 'date',
        'value' => date('Ymd'),
        'orderby' => 'meta_value_num',
            'meta_key' => 'event_date',
            'order' => 'ASC',
            'meta_query' => $meta_query,
    );
    $query = new WP_Query( $args );

    if( $query->have_posts() ) : 
        while( $query->have_posts() : $query->the_post();

            // post template goes here

        endwhile;
    endif;
    wp_reset_postdata();

    restore_current_blog();
}

and the code above produce :

  • Post 1 (From Sub Site 1)
  • Post 2 (From Sub Site 1)
  • Post 1 (From Sub Site 2)
  • Post 2 (From Sub Site 2)
  • Post 1 (From Sub Site 3)
  • Post 2 (From Sub Site 3)
  • Post 1 (From Sub Site 4)
  • Post 2 (From Sub Site 4)

And for the next page it produce this :

  • Post 3 (From Sub Site 1)
  • Post 4 (From Sub Site 1)
  • Post 3 (From Sub Site 2)
  • Post 4 (From Sub Site 2)
  • Post 3 (From Sub Site 3)
  • Post 4 (From Sub Site 3)
  • Post 3 (From Sub Site 4)
  • Post 4 (From Sub Site 4)

Since I'm using 'posts_per_page' = 2, what I really want is showing post ordered by date from all multisite, like this :

  • Post 1 (Fetch from the latest event date regardless from which Sub Site this come from )
  • Post 2 (Fetch from the latest event date regardless from which Sub Site this come from )

And next page :

  • Post 3 (Fetch from the latest event date regardless from which Sub Site this come from )
  • Post 4 (Fetch from the latest event date regardless from which Sub Site this come from )

What is the best solution to manage this scenario? Since I want to paginate the posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.