Sarathlal N

Display all sites in a network as a drop down list – WordPress

When creating WordPress network sites, regularly I want to display all sites in that network as a drop down in front end.

We can use a simple code snippet that useĀ wpdb class to get such a list.

global $wpdb;
$blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'"));
	 <select onChange="window.document.location.href=this.options[this.selectedIndex].value;" value="GO">
		 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))){ //This if statement that hide main site from list. So comment or uncomment this line with below closing tag
					<option value="<?php echo $addr; ?>"><?php echo $name;?></option>
					} //End of if statement

Need Help?

Do you need any help on WordPress? I'm happy to help you.

Contact Me

Recent Posts

  1. Get variation product data on selecting variation options - WooCommerce
  2. Add TinyMCE WYSIWYG (Rich text area) meta box - WordPress
  3. Reorder product tabs in single product page - WooCommerce
  4. Download webpage for offline reading - wget
  5. Remove product tabs in single product page - WooCommerce

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about them, please get in touch.