WordPress.org

Forums

How to get post content from it's ID in multi site? (3 posts)

  1. heyDude
    Member
    Posted 12 months ago #

    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?

  2. dcleek
    Member
    Posted 11 months ago #

    Have you tried using WP_Query's Orderby random parameter?

    http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Also, it looks like you should add the_content() in your foreach loop to pull content along with the title.

    https://codex.wordpress.org/Function_Reference/the_content

  3. heyDude
    Member
    Posted 11 months ago #

    Thanks for your reply. Yes I have tried WP_Query's Orderby parameter and what that would do is pull out the random posts from blog1 and then pull out random posts from blog2 due to foreach Loop. I haven't tried the second solution yet but I will soon

    Thanks

Reply

You must log in to post.

About this Topic