I'm trying to show all posts from the sub sites on my WordPress Multisite installation

Hi There!

I'm developing locally on my mac and I'm trying to figure out something to do with loops.

My domains are:

domain.dev
minidomainone.domain.dev
minidomaintwo.domain.dev

What I'm trying to achieve is creating a loop/query on domain.dev to show the posts from the other two domains.

I don't want to use (switch_to_blog) as that's a drain on server resources, I want to use the "network_query_posts" but just can't seem to figure it out.

Look forward to hearing from you!

Joe,

  • Alexander

    Hi @Joe,

    In theory, the network query class is designed to work just like the standard wp_query

    Are your posts already indexed? I'd start by doing some testing with the global posts widget: https://premium.wpmudev.org/project/recent-global-posts-widget/#usage

    This way you can be sure that Post Indexer is working properly. Then maybe you can check the code of that plugin and borrow it's usage of the network query.

    You can see an example of how that look works in widget-recent-global-posts.php around line 144

    Best regards,

  • Joe

    Hi @Alexander Rohman,

    Thank you for your speedy reply! I'm very impressed!

    So this is the code I've taken from the file you mentioned and I've added it "home.php" instead of the loop that was there.

    <?php
    
    		$network_query = network_query_posts( array(
    			'post_type'      => $recentglobalpoststype,
    			'posts_per_page' => $recentglobalpostsnumber,
    		) );
    
    			if ( network_have_posts() ) :
    				echo '<ul>';
    				while ( network_have_posts() ) :
    					network_the_post();
    					echo '<li>';
    						$post = network_get_post();
    						$the_permalink = network_get_permalink();
    						$the_title = network_get_the_title();
    						$the_content = network_get_the_content();
    
    						$blog = get_blog_details( $post->BLOG_ID );
    						$blog_title = $blog ? $blog->blogname : '';
    						$title = substr( $the_title, 0, $recentglobalpoststitlecharacters );
    						$content = substr( strip_tags( $the_content ), 0, $recentglobalpostscontentcharacters );
    						switch ( $recentglobalpostsdisplay ) {
    							case self::DISPLAY_BLOG_CONTENT:
    								echo '<a href="', $the_permalink, '">', '[', $blog_title, ']</a>';
    								echo '<br>';
    								echo $content, $recentglobalpostscontentcharacters < strlen( $the_content ) ? '&hellip;' : '';
    								echo '<br><a href="', $the_permalink, '">', __( 'Read More', 'rgpwidget' ), ' &raquo;</a>';
    								break;
    							case self::DISPLAY_CONTENT:
    								echo $content, $recentglobalpostscontentcharacters < strlen( $the_content ) ? '&hellip;' : '';
    								echo '<br><a href="', $the_permalink, '">', __( 'Read More', 'rgpwidget' ), ' &raquo;</a>';
    								break;
    							case self::DISPLAY_TITLE:
    								echo '<a href="', $the_permalink, '">', $title, '</a>';
    								break;
    							case self::DISPLAY_TITLE_BLOG:
    								echo '<a href="', $the_permalink, '">', $title, ' [', $blog_title, ']</a>';
    								break;
    							case self::DISPLAY_TITLE_BLOG_CONTENT:
    								echo '<a href="', $the_permalink, '">', $title, ' [', $blog_title, ']</a>';
    								echo '<br>';
    								echo $content;
    								break;
    							case self::DISPLAY_TITLE_CONTENT:
    							default:
    								echo '<a href="', $the_permalink, '">', $title, '</a>';
    								echo '<br>';
    								echo $content;
    								break;
    						}
    					echo '</li>';
    				endwhile;
    				echo '</ul>';
    			endif;
    		echo $args['after_widget'];
    	}
    
    	?>

    Obviously that doesn't work because I'm assuming a lot of that code does not to be there for the purpose of display all posts from all sites on the homepage of the main one.

    Any chance you could tag a developer to come and have a look, this question seems to have been asked a few times but the solution has never been shared.

    Look forward to hearing from you!

    Joe,

  • Alexander

    Hi @Joe,

    Thanks for the additional info. Before I get in touch with the dev here, I did notice a syntax error. You have an extra curly brace.

    You should be able to remove this code:

    echo $args['after_widget'];
    	}

    Also, are you developing with WP_DEBUG enabled? This would help you find these error messages.

    Let me know if this helps, if not I'll request that the developer take a look here.

    Best regards,

  • Joe

    Hi @Alexander Rohmann,

    Thanks for pointing that out! However it still makes no difference!

    If you could get in touch with the dev that would be awesome! It's such a shame, you guys make such awesome plugins like the Post Indexer one but provide no real documentation!

    Surely being able to have a loop that calls all posts from the network is something people would find very useful. Your plugins that do something similar are too standard, there is no real way to customises the way they output posts so it matches that of the site.

    Anyways, look forward to hearing from you!

    Also, does WPMU dev recommend any good developers available to hire for projects that are built more or less entirely from your plugins?

    Joe,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.