WordPress ninjas… I require some help regarding pagination.
I have the following code :
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array_merge( $wp_query->query_vars,
array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
How come $query->max_num_pages
returns 0
?
And is there a way to alter the original archive query with the $query->max_num_pages
?
[EDIT]
In Addition to the above code I have the following
$output = '<div class="download-attachments left_content_container"><ul>';
//this does not retrieve anything
$query = new WP_Query( $args );
// this does retrieve posts
$attachments = get_posts( $args );
// this block is only used for testing purposes..
// ie. it is not being executed because the query does not return anything, Whilst get_posts() works
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo the_ID();
}
}
foreach ($attachments as $attachment) {
setup_postdata($attachment);
//get attachments metadata etc.
$url = wp_get_attachment_url( $attachment->ID );
$title = get_the_title( $attachment );
$caption = $attachment->post_excerpt;
$size = get_readable_size( filesize(get_attached_file( $attachment->ID )) );
$icon = get_icon_for_attachment( $attachment->ID );
$mime = get_post_mime_type( $attachment->ID );
$date_added = $attachment->post_date;
$filename = ( !$caption == '' ) ? $caption : $title ;
$description = ($attachment->post_content != '') ? '<span class="attachment-author"><span class="attachemnt-label">Added by: ' . $attachment->post_content . '</span></span>' : '';
$output .= '<li class="'.$mime.'">
<img class="attachment-icon" src="'.$icon.'" alt="pdf">
<a href="'. home_url().'/wp-content/plugins/download-attachments/includes/download.php?id='.$attachment->ID.'" class="attachment-link" title="'.$filename.'">'. $filename .'</a> '.$description.'
<span class="attachment-date"><span class="attachment-label">Date added: </span>'.$date_added.'</span>
<span class="attachment-size"><span class="attachment-label">Attachment size: </span>'.$size.'</span></li>' ;
}
$output .= '</ul></div>';
echo $output;
echo '<div class="paging pull-left">';
$paged = $wp_query->get( 'paged' );
if ( ! $paged) {/* do nothing */ ;}
else { bootstrap_pagination(); var_dump($wp_query->max_num_pages); // returns 0 }
echo '</div>';
You have a couple of problems here. You don’t need to call the global
$wp_query
because you are not going to use it. Secondly,array_merge
is totally out of place. This should not be used at all in this situation. Your code should look something like thisFor more info on custom queries with
WP_Query
have a look here