Paginate Multisite Attachment WP_Query

Website Developing, WordPress

I am working on a multisite blog network where I need to display x post per page and navigation at the of the bottom list. My code will not display a pagination at the bottom and displays x post per page after every multisite blog loop. I have tried moving the WordPress function paginate_links() outside the foreach loop and without success. Does anybody have a fix or workaround?

// Iterate through your list of blogs
foreach ($blog_ids  as $id){

// Switch to the next blog in the loop.    
switch_to_blog($id);
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array (                 
    'posts_per_page'        => $limit,
    'paged'                 => $paged,      
    'post_type'             => 'attachment',        
    'post_status'           => 'inherit',       
    'post_mime_type'        => 'application/pdf',
    'order'                 => 'ASC'
);

$pdf_attachments = query_posts( $args );

// See if there are PDF attachments.
if( have_posts() )
{   

    while( have_posts()) { the_post();

        $title = get_the_title( $pdf_attachments->blog_id );

        $url = wp_get_attachment_url( $pdf_attachments->blog_id );                          

        $list .= '<li>' 

        .'<a href="'. $url .'" title="' . $title . '" class="pdf" target="_blank">'. $title .'</a>'

        .'</li>';

    }           

    // Switch back to the main blog
    restore_current_blog();
}

return '<ul class="unstyled">' 
    . $list 
    . '</ul>' 
    . paginate_links( array(
            'base' => '%_%',                                
            'format' =>  $slug .'/page/%#%',
            'add_args' => array(
                'searchterm' => get_query_var('s'),
                'limit' => get_query_var('posts_per_page')
            )
        )
    );
}

You’re in luck! I have been working on a solution. Finally here is a version of my solution. I hope this helps anybody that is stuck with a WPMU plugin development that needs a pager.

<?php 
function network_pager() 
{
	
	global $post;		
	
	$blog_name = $blog_count = $searchterm = $list = ''; 
	
	$site_names = $get_site_names = $blog_id = $get_blog_ids = array();
	
	$searchterm =  $_GET['searchterm'];
	
	$limit = 20;
	
	$current_site = get_bloginfo('url');		
	
	$slug = $current_site. '/' .get_post( $post )->post_name;		
		
	$current_blog_id = get_current_blog_id();	
	
	$selected_blog = ( isset($_GET['selected_blog']) && $_GET['selected_blog'])? $_GET['selected_blog']: $current_blog_id;	
	
	$args = array(
		
		'network_id' => $wpdb->siteid
		
	);
	
	// array of blogs
	$network_site_ids = wp_get_sites( $args ); 
	
	foreach( $network_site_ids as $key => $network_site ) 
	{
			
		$blog_id = $network_site['blog_id'];
		
		$blog_title = get_bloginfo('name');
		
		$site_names = get_blog_details($blog_id);		
		
		$get_site_names[$key] = $site_names->blogname;				
		
		$get_blog_ids[$key] = $site_names->blog_id;
		
	}
		
	if( isset($selected_blog)) {				
			
		global $wpdb;
		
		switch_to_blog( $selected_blog );				
		
		$blog_details = get_blog_details( $selected_blog );
		
		$blog_name = $blog_details->blogname;
		
		$paged = get_query_var('paged') ? get_query_var('paged') : 1;				
			
		$orderby = 'name';				
		
		$args = array ( 
		
			'posts_per_page' 		=> $limit,
			
			'paged' 				=> $paged,
			
			'post_type' 			=> 'attachment',
			
			'post_status' 			=> 'inherit',
			
			'post_mime_type' 		=> 'application/pdf',			
			
			's'						=> $searchterm,
			
			'orderby'               => $orderby,
			
			'order'                 => 'ASC'
			
		);
			
		$pdf_attachments = query_posts( $args );				
		
		$query2 = new WP_Query( $args );
	
		// See if there are PDF attachments.
		if( have_posts() )
		{ 	
									
			while( have_posts()) { the_post();
				
				$title = get_the_title( $pdf_attachments->blog_id );
				
				$url = wp_get_attachment_url( $pdf_attachments->blog_id );							
			
				$list .= '<li>'	
				
				.'<a href="'. $url .'" title="' . $title . '" class="mtli_attachment mtli_pdf" target="_blank">'. $title .'</a>'
				
				.'</li>';

			}
			
			restore_current_blog();				
			
			$output .= '<div class="container clearfix">'
			
			. '<ul class="listings unstyled">' . $list . '</ul>'
			
			. '<!-- pagination here -->'

			.'<div class="pager_wrapper">'
			
			. paginate_links( array(
					'base' => '%_%',								
					'format' =>  $slug .'/page/%#%',
					'add_args' => array(
						'searchterm' => get_query_var('s'),
						'category' => get_query_var('media_category'),
						'limit' => get_query_var('posts_per_page'),
						'selected_blog' => $selected_blog
					)
				)
			)
													
			.'</div>'						         
			
			.'</div>'
			
			. wp_reset_query(); // end return					
		
		}
		else { 
			
			$output .= "<li>We're Sorry! We do not have PDFs for this department.</li>";
			
		}
		
	}
	
	return $output;			
			
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s