WordPress category pagination issue

I am building a Wordpress theme which uses the default posts and 3 extra post types. So far so good, all working ok. I have used the standard custom post type function to call all 3 extras. I have made static pages for each of these CPTs + default Posts. All of these are working Ok and the pagination works fine. I've applied the same code to category.php. However... I can't quite work out why the pagination isn't working on the category page properly. It works for the default 'out the box' Wordpress category (Uncategorised which I've changed to General) absolutely fine but not for any other categories. So in practice the code is good but if I look at another category rendering, it has not counted the number of posts correctly and is adding way too many numbers to the pagination and is throwing a 404 on the 2nd page onwards. However I think I've missed out some vital function command to explicitly include all categories. Do I need to add something to the query->set in the last function example below to make it work? What have I not done? I've read so many articles on here and elsewhere to try and fix the problem but to no avail so far. I think I'm not joining up the logic somewhere The 3 CPTs includes in functions.php : 'has_archive' => true, 'taxonomies' => array('category','post_tag' ),'show_in_rest' => true, The query for each static index page includes this query <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => '[CUSTOM POST TYPE]', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'paged' => $paged, 'posts_per_page' => 12, ); $loop = new WP_Query( $args ); $counter = 1; // Initialize counter outside the loop if ( $loop->have_posts() ): while ( $loop->have_posts() ) : $loop->the_post(); $class = ($counter % 2 === 0) ? 'ifatr-card--left-image' : 'ifatr-card--right-image'; ?> (I know I have two posts per page going on there btw but I think it's ignoring the 2nd one). I have the posts set to 12 in Wordpress settings. FYI that counter is there to make sure that the display is alternating the images right and left and is also working well. I then have a pagination that works just fine and is called from any page by: <?php pagination_bar( $loop ); ?> In this calls this in functions.php function pagination_bar( $query_wp ) { $pages = $query_wp->max_num_pages; $big = 999999999; // need an unlikely integer if ($pages > 1) { $page_current = max(1, get_query_var('paged')); echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => $page_current, 'total' => $pages, 'prev_text' => __( '« <span>Previous</span>' ), 'next_text' => __( '<span>Next</span> »' ), )); } } So far so good... All the categories are shared between all the custom post types and the default Posts. The category pages are displaying OK. The categories are shared OK and updating globally OK when edited. I added this to functions.php to make sure that the category page is including all the custom post types. // add custom post types for default category template function custom_post_type_cat_filter($query) { if ( !is_admin() && $query->is_main_query() ) { if ($query->is_category()) { $query->set( 'post_type', array( 'post','[CUSTOM POST TYPE 1]','[CUSTOM POST TYPE 2]','[CUSTOM POST TYPE 3]') ); } } } add_action('pre_get_posts','custom_post_type_cat_filter');

Comment (0)

You’ll be in good company