0

I am trying to implement pagination for category items listing archive.php page. But this does not work. Below is the code that I am using.

Custom Query:

$cat = single_cat_title("", false);
$cat_ID = get_cat_ID ($cat);

$args = array(
    'posts_per_page'    => '2',
    'post_type' => 'mathematics',
    'cat' => $cat_ID,
    'paged' => get_query_var('paged',1)
);
$posts = new WP_Query($args);

Pagination code:

echo paginate_links(array(
              'base'               => '%_%',
              'format'             => '?paged=%#%',
              'total' => $posts->max_num_pages,
              ));

When the page is loaded, it displays the pagination links but when I click on next it gives 404 error.

main page link: http://mk.local/category/mathematics/average/ when next is clicked : http://mk.local/category/mathematics/average/?paged=2

I have already tried updating permalinks

2
  • I have tried below options still it does not work. – Manjulam Kumar Jul 15 '19 at 5:02
  • When I print $wp_query, I do see a lot of differences. – Manjulam Kumar Jul 15 '19 at 5:06
0

You should use the pre_get_posts action to get the correct number of category pages and links.
The complete solution is explained here.

0

Remove all your pagination and try something like this, where mytheme is the name of your theme

   <?php
$GLOBALS['wp_query']->max_num_pages = $custom_query->max_num_pages;
the_posts_pagination(array(
    'next_text' => '<span class="meta-nav" aria-hidden="true">' . __('Next Post', 'mytheme') . '</span> ' . '<span class="screen-reader-text">' . __('Next post:', 'mytheme') . '</span> <br/>' . '<span class="post-title">%title</span>',
    'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __('Previous Post', 'mytheme') . '</span> ' . '<span class="screen-reader-text">' . __('Previous post:', 'mytheme') . '</span> <br/>' . '<span class="post-title">%title</span>'
));
?>

Let me know how it goes

0

This guy shows how to create AJAX pagination...

https://artisansweb.net/load-wordpress-post-ajax/

Very easy to follow, effective results.

0
  1. $wp_query dump on page 1 (http://archive.local/category/mathematics/average)

    WP_Query Object ( [query] => Array ( [category_name] => mathematics/average ) [query_vars] => Array ( [category_name] => average [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [tag] => [cat] => 6 [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [post_type] => [posts_per_page] => 2 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => average ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( [0] => wp_term_relationships ) [queried_terms] => Array ( [category] => Array ( [terms] => Array ( [0] => average ) [field] => slug ) ) [primary_table] => wp_posts [primary_id_column] => ID ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => [meta_table] => [meta_id_column] => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array ( ) [clauses:protected] => Array ( ) [has_or_relation:protected] => ) [date_query] => [queried_object] => WP_Term Object ( [term_id] => 6 [name] => Average [slug] => average [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => category [description] => [parent] => 5 [count] => 6 [filter] => raw [cat_ID] => 6 [category_count] => 6 [category_description] => [cat_name] => Average [category_nicename] => average [category_parent] => 5 ) [queried_object_id] => 6 [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 2 [posts] => Array ( ) [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => a661d92bb8a6ebd714003f1268022535 [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) )

  2. $wp_query dump on page 2 (http://archive.local/category/mathematics/average/page/2/)

    WP_Query Object ( [query] => Array ( [paged] => 2 [category_name] => mathematics/average ) [query_vars] => Array ( [paged] => 2 [category_name] => average [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [tag] => [cat] => 6 [tag_id] => [author] => [author_name] => [feed] => [tb] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [post_type] => [posts_per_page] => 2 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => average ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( [0] => wp_term_relationships ) [queried_terms] => Array ( [category] => Array ( [terms] => Array ( [0] => average ) [field] => slug ) ) [primary_table] => wp_posts [primary_id_column] => ID ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => [meta_table] => [meta_id_column] => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array ( ) [clauses:protected] => Array ( ) [has_or_relation:protected] => ) [date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 2, 2 [posts] => Array ( ) [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => 1 [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 6ff57f0f1abfeb2f58bbec696009b6bf [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) )

I don't understand why post_type is 'post'. I tried adding pre_get_posts filter like below.

function custom_query(){
    global $wp_query;
    $wp_query-> set('post_type', 'maths');
}
add_action('pre_get_posts', 'custom_query');

this changes the post_type to 'maths'. However the issue still remains unresolved.

I have recently started Wordpress development and feel totally lost with this issue :(

Edit: I just added 5 'posts' under 'Average' category and the pagination working. So, it seems that some how it is considering the max_num_pages from 'posts' for pagination and not from my custom post.

0
0

I added below code in functions.php and it is working fine now.

function custom_query( $query ){

    if(is_category()){
        $query->set( 'post_type', array( 'math' ) );
        $query->set( 'posts_per_page', '2' );
    }
}
add_action('pre_get_posts', 'custom_query');

Thank you all for your suggestions.

0

You need to set max page of your custom query to the global before the the_posts_pagination, like

$GLOBALS['wp_query']->max_num_pages = $query->max_num_pages;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.