WP_Query_Multisite
WP_Query_Multisite copied to clipboard
Returning only main site posts
Hi, I encounter a similar problem with other way like yours to query the network. Maybe you'll have an idea. I use your class in a shortcode built for Visual Composer. And it returns only posts from the main site. Here's the code:
function events_vc( $atts, $content = null ) {
extract( shortcode_atts( array (
'compare' => '>=',
'posts_per_page' => '-1',
'order' => 'ASC',
'el_class' => '',
), $atts ));
include_once(TEMPLATEPATH . 'inc/multisite-query.php');
$all_events = new WP_Query( array(
'multisite' => '1',
'post_type' => 'events',
'post_status' => 'publish',
'meta_key' => 'date_debut',
'orderby' => 'meta_value',
'order' => $order,
'posts_per_page' => $posts_per_page,
'meta_query' => array(
array(
'key' => 'date_debut',
'value' => date('Ymd'),
'compare' => $compare == '<' ? '<' : $compare,
'type' => 'DATE',
),
),
));
$output = '';
if ( $all_events->have_posts() ) {
$output .= '<div class="events-list '.$el_class.'">';
$output .= '<h4>Test</h4>';
while ( $all_events->have_posts() ) : $all_events->the_post();
ob_start();
get_template_part('components/modules/event');
$output .= ob_get_clean();
endwhile;
$output .= '</div>';
wp_reset_postdata();
}
return $output;
}
add_shortcode( 'events_vc_output', 'events_vc');
You should not include the class file inside a shortcode function, instead include it in the root of your functions.php
file. The class implements several hooks that are not available if executd inside add_shortcode
.
include_once(TEMPLATEPATH . 'inc/multisite-query.php');
function events_vc($atts, $contents = null) {
// code here...
}
add_shortcode('events_vc_output', 'events_vc');
OK Done but not working too.
Is it working if you perform this query outside of the shortcode?
Good question. I didn't think about doing that because I need it inside obviously. And your guess is right : it works outside (some problems with the orderby but it works).
No no... I made the same mistake than another time. As I imported the posts from a site to the main, I thought it was working. But it returns only posts from the main website.
I must admit that I'm not completely sure that the class is working properly with the latest WP version, since it's been 2 years since my last commit. I'll have time to test it tomorrow and let you know. I can't tell by your code if there's something wrong on your end.
Thanks a lot Miguel for taking care of this issue.
Miguel, I'm using another way to achieve what I want. But still interested in this network query. Should be native within Wordpress ! For instance, I'm using switch_to_blog in a foreach loop. Then after that, I order the posts by date from the client side. And it does the trick.