Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_get_sites alternative, returning random sites WordPress

Well, Hello

I require a query similar to (the depreciated) wp_get_sites function, that will return a list of blogs on the wordpress mu network. From this list, the titles of 10 random ones should be displayed with a link. This code will be used in the footer to link to other partner blogs.

It should be displayed in the following format with a pipe separating

randomblogtitle | randomblogtitle| randomblogtitle ...

I do not want this as a plugin or shortcode, just the php

Answers (2)

2012-01-02

Arnav Joy answers:

write following in your functions.php

<?php

function get_recent_blogs($number_blogs=10)
{
global $wpdb;
$blog_table=$wpdb->blogs;
/*fetch blog_id,domain,path from wp_blogs table ,where the blog is not spam,deleted or archived order by the date and time of registration */
$query="select blog_id,domain,path from $blog_table where public='1' and archived='0' and spam='0' and deleted='0' order by registered desc limit 0,$number_blogs";

$recent_blogs=$wpdb->get_results($wpdb->prepare($query));

return $recent_blogs;

}
?>


then call this in your footer.php or the place you want to call names of sites

<ul class="recent-blogs">
<?php $recent_blogs=get_recent_blogs(5);
foreach($recent_blogs as $recent_blog):
$blog_url="";
if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' )
$blog_url="http://".$recent_blog->domain.$recent_blog->path;
else
$blog_url="http://".$recent_blog->domain.$recent_blog->path;
$blog_name=get_blog_option($recent_blog->blog_id,"blogname");
?>
<li>
<h3><a href="<?php echo $blog_url;?>"><?php _e( $blog_name)?> </a></h3>
<span><?php echo $blog_name?></span>
</li>
<?php endforeach;?>
</ul>


Arnav Joy comments:

also you can check this

http://codex.wordpress.org/WPMU_List_All_Blogs_Widget


Arnav Joy comments:

you can also test this function

in functions.php
<?php

function get_all_sites() {

global $wpdb;

// Query all blogs from multi-site install
$blogs = $wpdb->get_results("SELECT blog_id,domain,path FROM wp_blogs where blog_id > 1 ORDER BY path");

// Start unordered list
echo '<ul>';

// For each blog search for blog name in respective options table
foreach( $blogs as $blog ) {

// Query for name from options table
$blogname = $wpdb->get_results("SELECT option_value FROM wp_".$blog->blog_id ."_options WHERE option_name='blogname' ");
foreach( $blogname as $name ) {

// Create bullet with name linked to blog home pag
echo '<li>';
echo '<a href="http://';
echo $blog->domain;
echo $blog -> path;
echo '">';
echo $name->option_value;
echo '</a></li>';

}
}

// End unordered list
echo '</ul>';
}

?>

then in footer.php

<?php get_all_sites();?>


npeplow comments:

Thanks

This is nearly there

- I dont think either displays randomly (so a different set of ten appears each time)
- second get_all_sites() function does not limit to ten
- both are using unordered lists, instead of the pipe delimiter on a single row

you can see get_all_sites() running on legalgraduate.com

cheers
nick


Arnav Joy comments:

please explain what you want , also tell me which code worked for you ??

what does this point mean

get_all_sites() function does not limit to ten





npeplow comments:

hi Arnav

Both sets of code worked,

"From this list, the titles of 10 random ones should be displayed with a link."

- What I mean here is that from the list of 30 ish websites that are returned, 10 should be randomly picked and displayed - So every time the page loads the order and websites selected will be different

"It should be displayed in the following format with a pipe separating

randomblogtitle | randomblogtitle| randomblogtitle ..."

- What I want here is not an unordered list, but the ten 10 links returned on one line seperated by a pipe, for example if you look at the bottom of legalgraduate.com there is something similar to the below

Banking Graduate | Publishing Graduate | Manufacturing Graduate | Charity Graduate


Arnav Joy comments:

find following lines in functions.php

// Query all blogs from multi-site install
$blogs = $wpdb->get_results("SELECT blog_id,domain,path FROM wp_blogs where blog_id > 1 ORDER BY path");

place following code above it




try this in functions.php

this will give you random 10 entries


function get_all_sites() {

global $wpdb;


while(true){
$rand = mt_rand(2, 30);
if(!in_array($rand, $arr)){
$arr[] = $rand;
}
if(count($arr) == 10){
break;
}
}

$arr = implode(',',$arr);




// Query all blogs from multi-site install
$blogs = $wpdb->get_results("SELECT blog_id,domain,path FROM wp_blogs where blog_id IN (".$arr.") ORDER BY path");

// Start unordered list
echo '<ul>';

// For each blog search for blog name in respective options table
foreach( $blogs as $blog ) {

// Query for name from options table
$blogname = $wpdb->get_results("SELECT option_value FROM wp_".$blog->blog_id ."_options WHERE option_name='blogname' ");
foreach( $blogname as $name ) {

// Create bullet with name linked to blog home pag
echo '<li>';
echo '<a href="http://';
echo $blog->domain;
echo $blog -> path;
echo '">';
echo $name->option_value;
echo '</a></li>';

}
}

// End unordered list
echo '</ul>';
}

?>


Arnav Joy comments:

and now try this in functions.php

function get_all_sites() {

global $wpdb;


while(true){
$rand = mt_rand(2, 30);
if(!in_array($rand, $arr)){
$arr[] = $rand;
}
if(count($arr) == 10){
break;
}
}

$arr = implode(',',$arr);




// Query all blogs from multi-site install
$blogs = $wpdb->get_results("SELECT blog_id,domain,path FROM wp_blogs where blog_id IN (".$arr.") ORDER BY path");

// Start unordered list
echo '<p>';

// For each blog search for blog name in respective options table
foreach( $blogs as $blog ) {

// Query for name from options table
$blogname = $wpdb->get_results("SELECT option_value FROM wp_".$blog->blog_id ."_options WHERE option_name='blogname' ");
foreach( $blogname as $name ) {

// Create bullet with name linked to blog home pag
echo '<a href="http://';
echo $blog->domain;
echo $blog -> path;
echo '"> | ';
echo $name->option_value;
echo '</a>';

}
}


echo '</p>';
}

?>


Arnav Joy comments:

use this in functions.php

function get_all_sites() {

global $wpdb;


while(true){
$rand = mt_rand(2, 30);
if(!in_array($rand, $arr)){
$arr[] = $rand;
}
if(count($arr) == 10){
break;
}
}

$arr = implode(',',$arr);




// Query all blogs from multi-site install
$blogs = $wpdb->get_results("SELECT blog_id,domain,path FROM wp_blogs where blog_id IN (".$arr.") ORDER BY path");

// Start unordered list
echo '<p>';

// For each blog search for blog name in respective options table
foreach( $blogs as $blog ) {

// Query for name from options table
$blogname = $wpdb->get_results("SELECT option_value FROM wp_".$blog->blog_id ."_options WHERE option_name='blogname' ");
foreach( $blogname as $name ) {

// Create bullet with name linked to blog home pag
echo '<a href="http://';
echo $blog->domain;
echo $blog -> path;
echo '"> ';
echo $name->option_value;
echo '</a> | ';

}
}


echo '</p>';
}

?>

2012-01-02

Alberto Hornero Luque answers:

You need to get a list of blog IDs, and the function get_blog_list() is deprecated. I would use the function wp_get_sites() to achieve the goal.

I suggest you pass the 'sort_column => 'last_updated' argument, and 'limit' the results to 100 or something like that. This would make the next query much faster, and then displays the results at your convenience.


Alberto Hornero Luque comments:

Here a similar code could help you:

<?php

$pages = query_posts(array('post_parent' => 15, 'post_type' => 'page', 'meta_key' => 'testimonial', 'showposts' => 3, 'orderby' => rand));

foreach($pages as $child) {

$testimonial = get_post_meta($child->ID, 'testimonial', false);
$projName = get_the_title($child->ID);
$projLink = get_permalink($child->ID);

if ($testimonial) {

foreach ($testimonial as $testimony) {

$fullValue = explode("|", $testimony);

$docPic = $fullValue[0];
$docQuote = $fullValue[1];
$docName = $fullValue[2];

?>

<li class="testimonial">

<img src="<?php echo $docPic; ?>" alt="<?php echo $docName; ?>" title="<?php echo $docName; ?>" />

<p class="quote">&ldquo;<?php echo $docQuote; ?>&rdquo;</p>

<p class="cite"><span class="doc"><?php echo $docName; ?></span> | <a href="<?php echo $projLink; ?>" title="<?php echo $projName; ?>"><?php echo $projName; ?></a></p>

</li><!-- /.testimonial -->

<?php

}

}

}

?>


From: <em>http://wordpress.org/support/topic/display-get_pages-array-in-random-order</em>