Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Last active November 18, 2017 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bappi-d-great/bb9ee958a4d297dd218d to your computer and use it in GitHub Desktop.
Save bappi-d-great/bb9ee958a4d297dd218d to your computer and use it in GitHub Desktop.
Get All posts by tag in a network - wordpress multisite
<?php
/*
*
* Uses: get_tagged_post() or get_tagged_post( AN ARRAY OF BLOG IDs )
*
*/
function get_tagged_post($blogs = array(), $tag) {
if( count( $blogs ) < 1 ){
$blog_list = wp_get_sites();
foreach ( $blog_list as $blog ) {
array_push( $blogs, $blog['blog_id'] );
}
}
$posts = array();
foreach( $blogs as $blog ){
switch_to_blog( $blog );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag
)
)
);
$tagged_posts = get_posts( $args );
foreach( $tagged_posts as $tagged_post ){
array_push( $posts, array(
'title' => $tagged_post->post_title,
'link' => get_permalink( $tagged_post->ID )
) );
}
restore_current_blog();
}
?>
<ul class="tagged_posts">
<?php foreach( $posts as $post ) { ?>
<li><a href="<?php echo $post['link'] ?>"><?php echo $post['title']; ?></a></li>
<?php } ?>
</ul>
<?php
}
get_tagged_post( array(), 'popular' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment