-
-
Save staylor/84da29af561229357f07 to your computer and use it in GitHub Desktop.
Posts tagged in multiple sites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment