logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$7
wp_get_sites alternative, returning random sites

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

npeplow | 01/02/12 at 1:08am Edit
Tutorial: How to assign prize money


The experts have suggested, on average, a prize of $25 for this question.

(2) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/02/12
    1:24am
    Arnav Joy says:

    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>

    • 01/02/12 1:41am

      Arnav Joy says:

      also you can check this

      http://codex.wordpress.org/WPMU_List_All_Blogs_Widget

    • 01/02/12 1:48am

      Arnav Joy says:

      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();?>

    • 01/02/12 2:31am

      npeplow says:

      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

    • 01/02/12 2:36am

      Arnav Joy says:

      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




    • 01/02/12 2:41am

      npeplow says:

      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

    • 01/02/12 2:54am

      Arnav Joy says:

      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>';
      }

      ?>

    • 01/02/12 2:59am

      Arnav Joy says:

      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>';
      }

      ?>

    • 01/02/12 3:01am

      Arnav Joy says:

      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>';
      }

      ?>

  • avatar
    Last edited:
    01/02/12
    6:03am
    Alberto Hornero Luque says:

    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.

    • 01/03/12 6:42am

      Alberto Hornero Luque says:

      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: http://wordpress.org/support/topic/display-get_pages-array-in-random-order

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.