Last active
November 18, 2017 22:56
-
-
Save bappi-d-great/bb9ee958a4d297dd218d to your computer and use it in GitHub Desktop.
Get All posts by tag in a network - wordpress multisite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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