I have searched all over the web to find some solution, but none of them are working for me, can someone help me with this and explain what is wrong? I've spent 3 hours debugging and didn't find the issue.
Here is my code:
global $paged;
global $wp_query;
wp_reset_query();
$per_page = get_option('posts_per_page');
$cat_cars = get_term_by('slug', 'cars', 'category');
$page_links_total = ceil($cat_cars->count / $per_page);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'cars',
'post_status' => 'publish',
'cat' => $cat_cars->term_id,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => $per_page,
'paged' => $paged
);
$page_links = paginate_links(array(
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'end_size' => 2,
'mid_size' => 1,
'type' => 'plain',
'total' => $page_links_total,
'current' => $paged
));
$car_query = new WP_Query($args);
And later i call have_posts
, the_post
, etc..
This part works, until I go to the second page of my pagination and get nothing found.
$car_query->max_num_pages
for thetotal
argument ofpaginate_links()
? This will probably not solve your problem, but it saves one expensive DB query.