secret
Created

Posts tagged in multiple sites

  • Download Gist
tag-multisite.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
/**
* Alter the tags inline query so that it returns date in addition to ID
*
* @global hyperdb $wpdb
* @param string $fields
* @return string
*/
function _filter_fields_id_date() {
global $wpdb;
return "{$wpdb->posts}.ID, {$wpdb->posts}.post_date";
}
 
/**
* Alter the tags inline, remove the fields param
*
* @param WP_Query $query
*/
function _remove_fields_from_query( &$query ) {
$query->set( 'fields', '' );
}
 
/**
* Query and cache id, date, blog_id for posts in eMusic and 17 Dots sites
* tagged with the current tag
*
* @param boolean $all
* @return array
*/
function get_topic_post_IDs( $all = false ) {
$key = 'topic-posts-' . get_query_var( 'tag' );
$group = 'topics';
$posts = wp_cache_get( $key, $group );
 
if ( empty( $posts ) ) {
add_filter( 'posts_fields', '_filter_fields_id_date', 1000 );
add_action( 'parse_query', '_remove_fields_from_query', 1000 );
$emusic_data = _type_taxonomy_region_ids( '', get_query_var( 'tag' ) );
 
if ( empty( $emusic_data ) ) {
$emusic_data = array();
} else {
foreach ( $emusic_data as &$emusic_post ) {
$p = (array) $emusic_post;
$emusic_post = $p;
$emusic_post['blog_id'] = 1;
}
}
 
switch_to_blog( 3 );
$dots_data = _type_taxonomy_region_ids( 'post', get_query_var( 'tag' ) );
if ( empty( $dots_data ) ) {
$dots_data = array();
} else {
foreach ( $dots_data as &$dots_post ) {
$p = (array) $dots_post;
$dots_post = $p;
$dots_post['blog_id'] = 3;
}
}
remove_filter( 'posts_fields', '_filter_fields_id_date', 1000 );
remove_action( 'parse_query', '_remove_fields_from_query', 1000 );
 
restore_current_blog();
 
$times = array();
$posts = array();
foreach ( array_merge( $emusic_data, $dots_data ) as $i => $post ) {
$incr = $i;
$time = strtotime( $post['post_date'] );
if ( in_array( $time + $incr, $times ) )
$incr = $i + 5;
 
$posts[$time + $incr] = $post;
$times[] = $time + $incr;
}
 
krsort( $posts );
 
wp_cache_set( $key, $posts, $group, 120 );
}
 
if ( ! $all ) {
$start = (int) get_query_var( 'paged' ) ? ( get_query_var( 'paged' ) - 1 ) * get_query_var( 'posts_per_page' ) : 0;
$posts = array_slice( $posts, $start, get_query_var( 'posts_per_page' ) );
}
 
return $posts;
}
 
/**
* Returns IDs for posts queried by post_type, term, and taxonomy
*
* @access private
* @param mixed $post_type
* @param string $term
* @param string $taxonomy
* @return array
*/
function _type_taxonomy_region_ids( $post_type = 'post', $term = '', $taxonomy = 'post_tag' ) {
$the_term = get_term_by( 'slug', $term, $taxonomy );
if ( ! empty( $the_term ) ) {
if ( empty( $post_type ) && 1 === get_current_blog_id() )
$post_type = get_emusic_post_types();
$results = new WP_Query( array(
'fields' => 'ids',
'post_type' => $post_type,
'tax_query' => array(
get_region_tax_query(),
array(
'operator' => 'IN',
'taxonomy' => $taxonomy,
'field' => 'term_taxonomy_id',
'terms' => array( $the_term->term_taxonomy_id ),
'include_children' => false
),
'relation' => 'AND'
)
) );
return $results->posts;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.