WordPress

Menampilkan Recent Post pada Blog Multisite

Oleh Wak Jek• 5 Komentar Arsip

Menampilkan Recent Post pada Blog Multisite

1. Bikin fungsi ini di functions.php

/**
* List recent posts across a Multisite network
*
* @uses get_blog_list(), get_blog_permalink()
*
* @param int $size The number of results to retrieve
* @param int $expires Seconds until the transient cache expires
* @return object Contains the blog_id, post_id, post_date and post_title
*/
function wp_recent_across_network( $size = 10, $expires = 7200 ) {
if( !is_multisite() ) return false;

// Cache the results with the WordPress Transients API
// Get any existing copy of our transient data
if ( ( $recent_across_network = get_site_transient( 'recent_across_network' ) ) === false ) {

// No transient found, regenerate the data and save a new transient
// Prepare the SQL query with $wpdb
global $wpdb;

$base_prefix = $wpdb->get_blog_prefix(0);
$base_prefix = str_replace( '1_', '' , $base_prefix );

// Because the get_blog_list() function is currently flagged as deprecated
// due to the potential for high consumption of resources, we'll use
// $wpdb to roll out our own SQL query instead. Because the query can be
// memory-intensive, we'll store the results using the Transients API
if ( false === ( $site_list = get_site_transient( 'multisite_site_list' ) ) ) {
global $wpdb;
$site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id') );
set_site_transient( 'multisite_site_list', $site_list, $expires );
}

$limit = absint($size);

// Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
foreach ( $site_list as $site ) {
if( $site == $site_list[0] ) {
$posts_table = $base_prefix . "posts";
} else {
$posts_table = $base_prefix . $site->blog_id . "_posts";
}

$posts_table = esc_sql( $posts_table );
$blogs_table = esc_sql( $base_prefix . 'blogs' );

$query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table\n";
$query .= "\tWHERE $posts_table.post_type = 'post'\n";
$query .= "\tAND $posts_table.post_status = 'publish'\n";
$query .= "\tAND $blogs_table.blog_id = {$site->blog_id})\n";

if( $site !== end($site_list) )
$query .= "UNION\n";
else
$query .= "ORDER BY post_date DESC LIMIT 0, $limit";
}

// Sanitize and run the query
$query = $wpdb->prepare($query);
$recent_across_network = $wpdb->get_results( $query );

// Set the Transients cache to expire every two hours
set_site_transient( 'recent_across_network', $recent_across_network, 60*60*2 );
}

return $recent_across_network;
}

2. Tampiklan di template home

$recent_blog_posts = wp_recent_across_network(5);
foreach($recent_blog_posts as $recent) { ?>
echo '

  • '.$recent->post_title.'
  • ';
    ?>
    }
    ?>

    Kode ini berasal dari smashingmagazine, saya modifikasi sedikit.

    Tulisan ini sudah berumur: 13 tahun 3 bulan 29 hari. Informasi di dalamnya mungkin sudah tidak relevan, tapi nilai historisnya sangat berharga, terutama bagi saya sebagai penulisnya.

    (Dipulihkan dari arsip Facebook oleh Wak Jek)

    Tag Terkait:

    Komentar (5)

    Arsip Statis
    Hasyim Asyari
    terima kasih.. saya coba dulu mastah,,,
    Hasyim Asyari
    oiya mastah, kalo ditampilkan di recent post gimana ya mastah,, terima kasih.
    Hasyim Asyari
    maaf ni master wp, bingung ni.. mksdnya di tampilkan di template home gimana ya, di file apa pada template y master, page.php atau dimana ya? :(
    Wak Jek
    tergantung template nya, jadi template home itu bisa macem2, kalau gk front-page.php, home.php kalau kedua file tsb nggka ada berarti index.php lihat selengkapnya tentang template hierarchy http://codex.wordpress.org/.../1/18/Template_Hierarchy.png
    Hasyim Asyari
    terima kasih master..