WP_Query_Multisite icon indicating copy to clipboard operation
WP_Query_Multisite copied to clipboard

Returning only main site posts

Open vguenichon opened this issue 7 years ago • 8 comments

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 == '&lt;' ? '<' : $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');

vguenichon avatar Sep 13 '17 12:09 vguenichon

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');

miguelpeixe avatar Sep 13 '17 18:09 miguelpeixe

OK Done but not working too.

vguenichon avatar Sep 14 '17 07:09 vguenichon

Is it working if you perform this query outside of the shortcode?

miguelpeixe avatar Sep 14 '17 14:09 miguelpeixe

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).

vguenichon avatar Sep 14 '17 15:09 vguenichon

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.

vguenichon avatar Sep 14 '17 16:09 vguenichon

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.

miguelpeixe avatar Sep 14 '17 19:09 miguelpeixe

Thanks a lot Miguel for taking care of this issue.

vguenichon avatar Sep 15 '17 07:09 vguenichon

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.

vguenichon avatar Sep 22 '17 13:09 vguenichon