• heyDude

    (@heydude)


    I’m running a multi site and I want to pull post with certain categories from two blogs. Therefore I am running a loop for each of the blog to pull the posts as one of the category is in blog 1 while the other category is in blog 2.

    <?php $value = array(); ?>
            <?php
            		// Get the values from $_POST
            		$original_blog_id = get_current_blog_id(); // get current blog
    
            		$bids = array(1,2); // all the blog_id's to loop through  EDIT
    
            		foreach($bids as $bid):
                 	switch_to_blog($bid); 
    
            		  $args = array(
            			'tax_query' => array(
            			'relation' => 'AND',
            				array(
            					'taxonomy' => 'Music',
            					'field' => 'slug',
            					'terms' => array('artist', 'club')
            				),
            					)
            				);
    
            		$the_query = new WP_Query( $args );
    
            ?>
    
            <?php $postids = array(); ?>
    
            <?php if ( $the_query->have_posts() ) { ?>
    
                <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
    
                	<?php $postids[]=get_the_ID(); ?>
    
                <?php endwhile; ?>
    
                 <?php $value[] = $postids; ?>
            <?php
                } else {
                    // no posts found
                    echo 'Nothing found.';
                }  
    
            ?>
        <?php endforeach; ?>
    
        <?php 
    
        	$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($value));
        	$list = iterator_to_array($it,false);
    
        	$posts = new WP_Query(array(
        		'post__in' => $list,
        		'post_type' => 'post',
        		));
    
        ?>
    
        <?php
        	foreach ($posts as $post) {
            setup_postdata($post);
        ?>
    
        <?php echo the_title(); ?>
        <?php
        }
    
         wp_reset_postdata();     ?>
    
        <?php switch_to_blog($original_blog_id);  ?>

    The reason why I’m getting the IDs inside an array:

    <?php $postids[]=get_the_ID(); ?>

    because I want to fetch random post’s. If at this point instead of the above statement I get the title and content of the posts then it will show in sequential order. Something like this:

    BLOG1: POST1,POST2, POST : BLOG2: POST1, POST2, POST3

    But I want Posts in random order like this:

    BLOG1: POST1, BLOG2: POST3, BLOG1: POST2, BLOG2: POST1: BLOG2: POST2

    So everything is working fine, I am able to get the posts IDs even outside the foreach loop but the problem is:

    I am not able to get post content from those IDs. It only gives me posts from blog 2 because the current blog is 2. But it doesn’t show anything from blog1 even though the postID is in the list array.

    Can anyone please help?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get post content from it's ID in multi site?’ is closed to new replies.