Bill: WordPress Multi-site Building Tutorial (three): How to invoke the main station of the child station article content, SQL statement how to write?

Source: Internet
Author: User
Tags multisite

1, if you know the programming of a friend can SQL statements, and then add PHP functions and other operations can directly call the site's database information to achieve the desired purpose.

Since the use of SQL statements first have to understand the WordPress multi-site database, multi-site activation will be more than a few tables wp_site,wp_sitemeta,wp_blogs,wp_blog_versions the most important of which is WP_ Blogs This table, it saves each sub-site access address you create, as well as the creation and modification times, and so on. Another point is that when you create a sub-site will be more than a number of data tables, such as wp_2_posts,wp_2_options, and so on, the middle of the number 2 is actually your wp_blogs table inside the blog_id number . This gives me the convenience of getting the data for each site.

For example:

<?PHPfunctionWPMU_RECENT_POSTS_MU ($how _many=10,$how _long=0,$titleOnly=true,$begin _wrap= "\n<li>",$end _wrap= "</li>"){    Global $wpdb; Global $table _prefix;$counter= 0;//use different SQL statements first by judging whether to show the time interval    if($how _long> 0)    {        $blogs=$wpdb->get_col ("Select blog_id from$wpdb->blogs WHERE public = ' 1 ' and archived = ' 0 ' and mature = ' 0 ' and spam = ' 0 ' and deleted = ' 0 ' and last_updated >= Date_sub (Current_date (), INTERVAL$how _longDay) ORDER by last_updated DESC "); }Else{$blogs=$wpdb->get_col ("Select blog_id from$wpdb->blogs WHERE public = ' 1 ' and archived = ' 0 ' and mature = ' 0 ' and spam = ' 0 ' and deleted = ' 0 ' ORDER by last_updated D ESC "); }                //If multiple site IDs exist        if($blogs) {             Echo"<ul>"; foreach($blogs  as $blog) {//Here are the data tables that need to be used                $blogOptionsTable=$wpdb->base_prefix.$blog." _options ";$blogPostsTable=$wpdb->base_prefix.$blog." _posts ";$options=$wpdb->get_results ("Select Option_value from$blogOptionsTableWHERE option_name in (' SiteURL ', ' Blogname ') ORDER by option_name DESC ");//get the title and ID number for the latest article                if($how _long> 0)                  $thispost=$wpdb->get_results ("Select ID, Post_title from$blogPostsTableWHERE post_status = ' publish ' and ID > 1 and post_type = ' post ' and post_date >= date_sub (current_date (), INTERVAL$how _longDay) ORDER by ID DESC LIMIT 0,1 "); }Else{$thispost=$wpdb->get_results ("Select ID, Post_title from$blogPostsTableWHERE post_status = ' publish ' and ID > 1 and post_type = ' post ' ORDER by ID DESC LIMIT 0,1 '); } //If there is an input content                 if($thispost)                  {                 //Get child site article links                 $thispermalink= Get_blog_permalink ($blog,$thispost[0]->id];if($titleOnly==false) {Echo $begin _wrap.‘ <a href= "'.$thispermalink.‘" > '.$thispost[0]->post_title. ' </a> <br/> by <a href= "'.$options[0]->option_value. '] > '.$options[1]->option_value. ' </a> '.$end _wrap;$counter++; }Else{Echo $begin _wrap.‘ <a href= "'.$thispermalink.‘" > '.$thispost[0]->post_title. ' </a> '.$end _wrap;$counter++; } }//judge the number of articles.                  if($counter>=$how _many) { Break; } } Echo"</ul>"; } }?>

Put the above code in the functions.php , the use of the method is described above. If you have 10 child sites. To display the latest article on this 10 subweb, you can use the method above:

true, ' <li> ', ' </li> ');?>

If you have 50 sub-sites, and you control the display of the latest article number is only 10, it is likely that more than 10 users at the same time using subsites to publish articles, then some of the latest articles can not be displayed, so consider the random display of the latest articles of the subweb. And each refresh of the page will randomly display a different subsite article code is as follows:

<?PHPfunctionLei_multsite_recent_post ($showpost) {    Global $wpdb,$post,$table _prefix;//First, all the subweb ID numbers are displayed.        $most _post=$wpdb->get_col ("Select blog_id from$wpdb->blogs WHERE public = ' 1 ' and archived = ' 0 ' and mature = ' 0 ' and spam = ' 0 ' and deleted = ' 0 ' ORDER by last_updated D ESC "); foreach($most _post  as $key=$v)         {            if($v!=1)//In addition to the primary site ID{$b _id[$key] =$v; } }        $newid=Array_flip($b _id);//PHP Array function swap key value        $new _post_id=Array_rand($newid, 2);//PHP Array functions randomly display 2, where the number cannot be greater than the number of child sites you create.         foreach($new _post_id  as $k=$vol )        {            $blogPostsTable=$wpdb->base_prefix.$vol." _posts ";$recent _post=$wpdb->get_results ("SELECT * FROM$blogPostsTablewhere post_status = ' publish ' and Post_type = ' post ' ORDER by post_date desc LIMIT 0,$showpost"); foreach($recent _post  as $ks=$vs)            {                $thispermalink= Get_blog_permalink ($vol,$vs-ID); $count 1+ = 1; Echo' <li><a href= '.$thispermalink.‘" > '.$vs->post_title. ' </a></li> '; }     }}?>

The above method is also placed in the functions.php file You use the theme, the use of the method as long as

<?php lei_multsite_recent_post (1);?>

2, but do not know the programming of friends will think of using plug-ins to complete, although sometimes to meet our requirements, but after all, the use of plug-ins have limitations, not every plug-in is appropriate (the code inside the plugin is also done with SQL statements). For example, there are several plugins:multisite Latest Posts widgets (show multi-site latest article) and multisite Featured blog (show multi-site featured blog) and WPMU Recent Posts Widget (displays multi-site recent articles)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.