public
Last active

Some handy wordpress multisite functions

  • Download Gist
multisite_functions.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
<?php
$posts = multisite_latest_post( array(
"how_many"=>10,
"how_long_days"=>30,
"how_many_words"=>50,
"more_text"=>"[...]",
"remove_html"=>true,
"sort_by"=>"post_date",
// if paginating:
"paginate"=>true,
"start"=>0,
"end"=>25,
// query based on meta values: (key / value)
"metas"=>array(
"tumblog"=>"articles",
)
));
echo "There were ".$posts['total']." posts found";
foreach($posts['posts'] as $item){
?>
<li><a href="<?php echo $item->post_url; ?>">++ <?php echo $item->post_title; ?></a><span> <?php echo multisite_post_meta($item->blog_id,$item->ID,"tumblog"); ?></span></li>
<?php
}
function multisite_latest_post($args) {
global $wpdb;
extract($args);
// Validate
if( empty($how_many) ) $how_many = 10;
if( empty($how_long_days) ) $how_long_days = 30;
if( empty($how_many_words) ) $how_many_words = 50;
if( empty($more_text) ) $more_text = "[...]";
if( empty($remove_html) ) $remove_html = true;
if( empty($post_type) ) $post_type = "video";
if( empty($sort_by) ) $sort_by = "post_date";
if( empty($paginate) ) $paginate = true;
if( empty($start) ) $start = 0;
if( empty($end) ) $end = 25;
if( !isset($metas) ) $metas = array();
$query = "SELECT blog_id FROM $wpdb->blogs WHERE blog_id !='1'";
$blogs = $wpdb->get_col($query);
 
if ($blogs) {
//we use blog id to loop post query
foreach ($blogs as $blog) {
$blogPostsTable = 'wp_'.$blog.'_posts';
$blogPostMetaTable = 'wp_'.$blog.'_postmeta';
 
$db_query = "SELECT sql_calc_found_rows $blogPostsTable.ID,
$blogPostsTable.post_author,
$blogPostsTable.post_title,
$blogPostsTable.guid,
$blogPostsTable.post_date,
$blogPostsTable.post_content,
$blogPostsTable.post_modified,
$blogPostsTable.comment_count
FROM $blogPostsTable";
 
$i = 1;
if( count($metas) ){
foreach($metas as $m){
foreach($m as $mname=>$mval){
$db_query .= " JOIN $blogPostMetaTable m{$i} ON m{$i}.post_id = $blogPostsTable.ID AND m{$i}.meta_key='{$mname}'";
$i++;
}
}
reset($metas);
$db_query .= " WHERE $blogPostsTable.post_status = 'publish' AND $blogPostsTable.post_type = '".$post_type."'";
$i = 1;
$mquery = array();
foreach($metas as $m){
foreach($m as $mname=>$mval){
$mquery[] = " m{$i}.meta_value='{$mval}' ";
$i++;
}
}
if( count($mquery) ){
$db_query .= " AND ( ".implode(" OR ",$mquery)." )";
}
}else{
$db_query .= " WHERE $blogPostsTable.post_status = 'publish' AND $blogPostsTable.post_type = '".$post_type."'";
}
if( $paginate ){
$db_query .= " LIMIT $start,$end";
}else{
$db_query .= " AND $blogPostsTable.post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long_days DAY)";
}
# echo $db_query."<br />";
$thispos = $wpdb->get_results($db_query);
foreach($thispos as $thispost) {
if($sort_by == 'post_date') {
$order = $thispost->post_date;
}
else{
$order = $thispost->post_modified;
}
 
$post_dates[] = $order;
$post_guids[$order] = $thispost->guid;
$blog_IDs[$order] = $blog;
$post_IDs[$order] = $thispost->ID;
$post_titles[$order] = $thispost->post_title;
$post_authors[$order] = $thispost->post_author;
$post_contents[$order] = $thispost->post_content;
$comments[$order] = $thispost->comment_count;
}
}
 
rsort($post_dates);
$union_results = array_unique($post_dates);
$ResultArray = array_slice($union_results, 0, $how_many);
 
foreach ($ResultArray as $date) {
$ID = $post_IDs[$date];
$id_author = $post_authors[$date];
$post_url = get_blog_permalink($blog_IDs[$date], $ID);/*$post_guids[$date];*/
$post_title = $post_titles[$date];
$post_content = $post_contents[$date];
$post_date = mysql2date(get_option('date_format'), $date);
$post_time = mysql2date(get_option('time_format'), $date);
$total_comment = $comments[$date];
$user_info = get_userdata($id_author);
$author_blog_url = get_blogaddress_by_id($user_info->primary_blog);
$author_url = $user_info->user_url;
$author_email = $user_info->user_email;
$blog_id = $blog_IDs[$date];
if($user_info->first_name) {
$author_name = $user_info->first_name.' '.$user_info->last_name;
}
else{
$author_name = $user_info->nickname;
}
 
if($remove_html) {
$post_content = multisite_cleanup_post($post_content);
}
 
$results = array();
 
$results['ID'] = $ID;
$results['post_url'] = $post_url;
$results['post_title'] = $post_title;
$results['post_content'] = multisite_cut_article_by_words($post_content, $how_many_words);
$results['post_content'] .= $more_text;
$results['author_blog_url'] = $author_blog_url;
$results['author_url'] = $author_url;
$results['author_email'] = $author_email;
$results['author_name'] = $author_name;
$results['post_date'] = $post_date;
$results['post_time'] = $post_time;
$results['comment_count'] = $total_comment;
$results['blog_id'] = $blog_id;
$returns[] = $results;
}
$latest_posts = multisite_bind_array_to_object($returns);
$total = $wpdb->get_var("SELECT FOUND_ROWS() as total");
return array("posts"=>$latest_posts,"total"=>$total);
}
}
 
function multisite_post_meta($blog, $post, $key){
global $wpdb;
$blogPostsTable = 'wp_'.$blog.'_postmeta';
$query = "SELECT meta_value FROM {$blogPostsTable} WHERE post_id ='{$post}' AND meta_key='{$key}'";
$meta_value = $wpdb->get_var($query);
return $meta_value;
}
 
function get_blog_domain($blog_id){
global $wpdb;
$query = "SELECT domain FROM $wpdb->blogs WHERE blog_id ='{$blog_id}'";
$domain = $wpdb->get_var($query);
return $domain;
}
 
function multisite_bind_array_to_object($array) {
$return = new stdClass();
 
foreach ($array as $k => $v) {
if (is_array($v)) {
$return->$k = multisite_bind_array_to_object($v);
}
else {
$return->$k = $v;
}
}
return $return;
}
 
function multisite_cut_article_by_words($original_text, $how_many) {
$word_cut = strtok($original_text," ");
 
$return = '';
 
for ($i=1;$i<=$how_many;$i++) {
$return .= $word_cut;
$return .= (" ");
$word_cut = strtok(" ");
}
 
$return .= '';
return $return;
}
 
function multisite_cleanup_post($source) {
$replace_all_html = strip_tags($source);
$bbc_tag = array('/\[caption(.*?)]\[\/caption\]/is');
$result = preg_replace($bbc_tag, '', $replace_all_html);
 
return $result;
}
 
function multisite_get_option($blog_id,$key){
switch_to_blog($blog_id);
$val = get_option($key);
restore_current_blog();
return $val;
}
 
function multisite_get_thumb($postID, $w = 200, $h = 150, $blogID = 1, $link = true, $return = false) {
switch_to_blog($blogID);
$scriptpath = get_bloginfo('template_directory');
if( $thumbnail = get_post_meta($postID, 'thumbnail', true) ){
$iurl = '/wp-content/iptv/img/'.$thumbnail;
}else{
$images = get_children(array('post_parent' => $postID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
if ( $images ){
$img = array_shift($images);
$imagelink = wp_get_attachment_image_src($img->ID,array($w,$h));
$iurl = $imagelink[0];
}
}
$out = '';
if( $iurl ){
$img = $iurl;
if( $link ) $out .= '<a href="'.get_permalink($postID).'" title="Permanent Link to '.get_the_title($postID).'">';
$out .= '<img src="'.$img.'" width='.$w.' height='.$h.' />';
if( $link ) $out .= '</a>';
}
restore_current_blog();
if($return) {
return $out;
} else {
echo $out;
}
}
 
function get_thumb($postID, $w = 200, $h = 150, $link = true, $return = false) {
$scriptpath = get_bloginfo('template_directory');
if( $thumbnail = get_post_meta($postID, 'thumbnail', true) ){
$iurl = '/wp-content/iptv/img/'.$thumbnail;
}else{
$images = get_children(array('post_parent' => $postID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
if ( $images ){
$img = array_shift($images);
$imagelink = wp_get_attachment_image_src($img->ID,array($w,$h));
$iurl = $imagelink[0];
}
}
$out = '';
if( $iurl ){
$img = $iurl;
if( $link ) $out .= '<a href="'.get_permalink($postID).'" title="Permanent Link to '.get_the_title($postID).'">';
$out .= '<img src="'.$img.'" width='.$w.' height='.$h.' />';
if( $link ) $out .= '</a>';
}
if($return) {
return $out;
} else {
echo $out;
}
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.