Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active January 19, 2022 22:00
Show Gist options
  • Save Jany-M/d3014a7039c884a8bbd8e8271cfcd982 to your computer and use it in GitHub Desktop.
Save Jany-M/d3014a7039c884a8bbd8e8271cfcd982 to your computer and use it in GitHub Desktop.
[WP MU] Shortcode for custom loops between different blogs - For WordPress Multisite MU
<?php
// This is a work in progress, but it works fine
// Requires custom functions & libraries not present in the script (eg. Swiper)
// Adapted from my Visual Composer custom module (same purpose) https://gist.github.com/Jany-M/7a94e2edc04969dcded247ff99a15088
// Doesn't play well with WPML, for now
function shambix_shortcode_mu_posts($atts, $content = null ) {
// Let's compare parameters with defaults
$atts = shortcode_atts( array(
'blog_id' => '1',
'title' => '',
'post_type' => 'post',
'number' => '',
'number' => '',
'post_order' => 'date',
//'show_date' => false,
//'show_featured' => true,
//'featured_size' => 'thumbnail',
//'featured_size_custom' => '',
'class' => '',
//'row_width' => true,
'tax' => '',
'tax_terms' => ''
), $atts );
// posts per page
if($atts['number']) {
$posts_per_page = $atts['number'];
} else {
$posts_per_page = get_option('posts_per_page');
}
// Blog IDs
if( $atts['blog_id'] ) {
$sites = explode(',',$atts['blog_id']);
}
// apply post types
if( $atts['post_type'] ) {
$post_types = explode(',',$atts['post_type']);
}
if( $atts['tax'] ) {
$tax = $atts['tax'];
}
if( $atts['tax_terms'] ) {
$terms = explode(',',$atts['tax_terms']);
}
// Current Blog ID is important
global $current_blog_id;
$current_blog_id = get_current_blog_id();
/*if(defined('ICL_LANGUAGE_CODE')) {
$lang = ICL_LANGUAGE_CODE;
} else {
$lang = 'it';
}*/
// Cat
/*if($cat != '') {
$tax_args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cat,
);
$args = array_merge( $args, $tax_args );
}*/
// ARGS
//$max = 12;
$latest_news_args = array(
//'posts_per_page' => $max,
//'post_type' => array('post'),
'posts_per_page' => $posts_per_page,
'post_type' => $post_types,
'post_status' => 'publish',
'orderby' => $atts['post_order'],
'order' => 'DESC',
);
// START
$html = '<div class="vc_multisite_posts ' . $atts['class'] . '">';
$html .= '<!-- Blog -->
<div class="blog">
<div class="triangolo_giallo left"></div>
<div class="container-fluid">';
if( $atts['title'] ) {
$html .= '<h3 class="h2_title">' . $atts['title'] . '</h3>';
}
$html .= '<div class="swiper-container">
<div class="swiper-wrapper">';
// Looping all the sites and adding posts
foreach( $sites as $site ) {
switch_to_blog( $site );
// RESETS
$i = $skipped = '0';
$target = '';
// Adjust depending on Blog ID
if(get_current_blog_id() != $current_blog_id) {
// Links target
$target = 'target="_blank"';
// WPML only show posts in same lang - DOESNT WORK
/*if(defined('ICL_LANGUAGE_CODE')) {
$latest_news_args['suppress_filters'] = false;
//$latest_news_args['suppress_filters'] = 'FALSE',
//global $sitepress;
//$sitepress->switch_lang($lang);
do_action( 'wpml_switch_language', $lang);
// Because WPML doesnt work
$latest_news_args['posts_per_page'] = -1;
}*/
}
// If tax + term
// https://wordpress.stackexchange.com/questions/189035/get-all-terms-inside-a-specific-taxonomy-in-a-multisite
if($tax != '' && $terms != '') {
$check_later = array();
global $wp_taxonomies;
$loop_tax = explode(',',$atts['tax']);
foreach($loop_tax as $taxonomy){
if (isset($wp_taxonomies[$taxonomy])){
$check_later[$taxonomy] = false;
} else {
$wp_taxonomies[$taxonomy] = true;
$check_later[$taxonomy] = true;
}
}
$tax_args = array(
'tax_query' => array(
'operator' => 'AND',
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $terms,
),
)
);
$latest_news_args = array_merge( $latest_news_args, $tax_args );
}
// QUERY THE POSTS
// Check if values are cached, if not cache them
$theme_slug = str_replace(' ', '_', THEME_SLUG);
$latest_news_transient = $theme_slug.'_latest_news_blogID_'.$current_blog_id.'_transient_24h';
//delete_transient($latest_news_transient);
if(get_transient($latest_news_transient) === false) {
$latest_news_transient_loop = new WP_Query($latest_news_args);
//Cache Results
set_transient($latest_news_transient, $latest_news_transient_loop, 24 * HOUR_IN_SECONDS );
}
$latest_news_transient_loop = get_transient($latest_news_transient);
$count = $latest_news_transient_loop->post_count;
// TODO: If more than one blog, need to merge all sites' posts into one big post result and limit with posts_per_page
if( $latest_news_transient_loop->have_posts() ) : while( $latest_news_transient_loop->have_posts() ) : $latest_news_transient_loop->the_post();
global $post;
$post_id = $post->ID;
$i++;
if(has_post_thumbnail($post_id)) {
$img_id = get_post_thumbnail_id($post_id);
$full_img_url = get_the_post_thumbnail_url($post_id,'full');
$full_img_src = wp_imager(400, 600, 1, null, false, $full_img_url, true);
}
// Cats
$post_categories = wp_get_post_categories($post_id);
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = '<li>'.$cat->name.'</li>';
}
$cats = implode(' ', $cats);
// WPML
/*if(defined('ICL_LANGUAGE_CODE')) {
//$translated_slug = apply_filters( 'wpml_get_translated_slug', 'product', 'product', $lang); // apply_filters doesnt work on MU
// Awful workaround because it wont query the correct lang -_-
$post_lang = apply_filters( 'wpml_post_language_details', NULL, get_the_ID());
if($post_lang["language_code"] != $lang) {
$skipped++;
continue;
}
}*/
$html .= '<div class="swiper-slide">';
$html .= '<div id="slide-'.$i.'" class="blog_item">
<div class="inner">
<div class="post-feature-overlay">
<div class="post-feature" data-responsive-background-image>
<img src="'.$full_img_src.'; ?>" srcset="'.wp_get_attachment_image_srcset( $img_id, 'medium' ).'" sizes="'.wp_get_attachment_image_sizes( $img_id, 'medium' ).'" />
</div>
<div class="post-overlay"></div>
</div>
<div class="post-info">
<ul class="post-categories">'.$cats.'</ul>
<h3><a href="'.get_the_permalink().'" alt="'.get_the_title().'">'.get_the_title().'</a></h3>
<hr />
<!-- <div class="post-date"><p> //the_time("j F Y")</p></div> -->
<div class="post-excerpt">
'.custom_excerpt(24).'
</div>
<div class="post-read-more">
<a class="btn btn-giallo" href="'.get_the_permalink().'">Leggi</a>
</div>
</div>
</div>
</div>';
$html .= '</div>';
endwhile; endif; wp_reset_query(); wp_reset_postdata();
// Debug
/*if(get_current_blog_id() != $current_blog_id) {
if(current_user_can('administrator')) {
echo '<pre>';
var_dump($skipped);
echo '</pre>';
}
}*/
/* Skipped diff langs posts - Integrate with new query then */
/*if($skipped > 0) {
$latest_news_args['posts_per_page'] = $skipped;
$latest_news_transient_loop = new WP_Query($latest_news_args);
if( $latest_news_transient_loop->have_posts() ) :
}*/
// RESET
// If tax + term
if($tax != '' && $terms != '') {
if (isset($check_later))
foreach($check_later as $taxonomy => $unset)
if ($unset == true)
unset($wp_taxonomies[$taxonomy]);
}
//restore_current_blog();
} // end foreach site
$html .= '</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>';
$html .= "<script type='text/javascript'>
// Swiper News
var mySwiper_News = new Swiper ('.blog .swiper-container', {
normalizeSlideIndex: true,
slidesPerView: 4,
slidesPerGroup: 4,
loopedSlides: 4,
spaceBetween: 30,
direction: 'horizontal',
loop: true,
grabCursor: true,
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
}
});
</script>";
$html .= '</div>
<div class="triangolo_giallo right"></div>
</div>';
return $html;
restore_current_blog();
}
add_shortcode('mu_posts', 'shambix_shortcode_mu_posts');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment