I have a question. I would like to know, how can i exclude specified site from a network Multisite?
Here is the code i am using to call out all the links for the sites in my multisite network.
<?php $bcount = get_blog_count(); global $wpdb; $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'")); if(!empty($blogs)) { ?>
<?php foreach($blogs as $blog) {
$details = get_blog_details($blog->blog_id);
if($details != false) {
$addr = $details->siteurl;
$name = $details->blogname;
if(!(($blog->blog_id == 1)&&($show_main != 1))) { ?>
<li><a href="<?php echo $addr; ?>"><?php echo $name;?></a></li>
<?php } } } ?><?php } ?>
I wish to exclude 1 or 2 specified sites in the future. Thank you!
*is that possible to add a simple code to the existing code in order to make it work?
I have a question. I would like to know, how can i exclude specified site from a network Multisite?
Here is the code i am using to call out all the links for the sites in my multisite network.
<?php $bcount = get_blog_count(); global $wpdb; $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'")); if(!empty($blogs)) { ?>
<?php foreach($blogs as $blog) {
$details = get_blog_details($blog->blog_id);
if($details != false) {
$addr = $details->siteurl;
$name = $details->blogname;
if(!(($blog->blog_id == 1)&&($show_main != 1))) { ?>
<li><a href="<?php echo $addr; ?>"><?php echo $name;?></a></li>
<?php } } } ?><?php } ?>
I wish to exclude 1 or 2 specified sites in the future. Thank you!
*is that possible to add a simple code to the existing code in order to make it work?
Share Improve this question edited Apr 28, 2020 at 5:04 Jornes asked Apr 28, 2020 at 1:48 JornesJornes 7535 gold badges12 silver badges31 bronze badges1 Answer
Reset to default 2I'd recommend using get_sites()
instead of crafting $wpdb
calls.
Add this code to your theme's functions.php
file.
function wpse365255_print_sites() {
$args = array(
'number' => 10000, // if you've got more than 10,000 sites,
//you can increase this
'public' => 1,
'spam' => 0,
'deleted' => 0,
'archived' => 0,
'site__not_in' => array( 1, 2 ),
// this will exclude sites with ID 1 and 2
);
$sites = get_sites( $args ); // will return an array of WP_Site objects
$list = '';
foreach ( $sites as $site ) {
$details = get_blog_details( $site->blog_id );
if ( ! empty( $details ) ) {
$list .= '<li>';
$list .= '<a href="' . $details->siteurl . '">';
$list .= $details->blogname;
$list .= '</a>';
$list .= '</li>';
}
}
if ( ! empty( $list ) ) {
echo '<ul>' . $list . '</ul>';
}
}
Then, in your footer.php
file, replace the code you originally posted with this:
<?php
wpse365255_print_sites();
?>
It's generally best to not define functions in template files, but instead put them in functions.php
and call them from the template files, which is what this will do. (Better still to hook them to an action or filter hook, but that's a lesson for another day.)
References
get_sites()
WP_Site
class