3

To go further in my knowledge of wordpress, I've been developing a theme from scratch, and using plugins ACF and CPT UI, in order to create a simple and intuitive backoffice interface.

My problem is on the front-end although. I've been creating pagination for the homepage (using a custom query) and it works perfectly. This is not the case when it comes to category pages (in a template named category.php).

I've been Googling for hours in order to find a solution, but everything seems related to permalinks, when I don't have any problems with permalinks (I'm using the default structure /%postname%/ ).

Here's the code for my category page, including the custom loop and pagination :

<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$my_videos = new WP_Query(array(
   'post_type' => 'video', 
   'category_name' => single_cat_title('', false), 
   'posts_per_page' => 6
)); 
 if ( $my_videos->have_posts() ) :  
  while ($my_videos->have_posts()) : $my_videos->the_post();
   show_video_thumbnails();
  endwhile; 
else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

<!-- pagination here -->
<?php   next_posts_link( 'Older Entries', $my_videos->max_num_pages );
        previous_posts_link( 'Newer Entries' );
        wp_reset_postdata(); 
?>

Everything works great on the first page of the category: pagination is available and shows "Older entries".

But when clicking the link, I'm being redirected to /category/mycategoryname/page/2 (mycategoryname being my category name), but it shows a 404 error;

Any help would be greatly appreciated. I feel like I'm missing something here.

  • what is the file name of your template? – Michael Aug 16 '16 at 16:46
  • @Michael category.php . I'll add that to the thread. – Tayax Aug 17 '16 at 6:16
  • This looks like a similar situation, use pre_get_post instead of custom loop: wordpress.stackexchange.com/questions/5477/… – ngearing Aug 17 '16 at 6:36
  • @Nath Worked perfectly! I invite you to post an answer and I'll validate it. You deserver the creds! – Tayax Aug 17 '16 at 6:57
  • @Tayax Thank you. That's most kind. I have posted my answer. – ngearing Aug 17 '16 at 23:25
5

As this is your main loop of your template, you should not be making a new loop, but modify the existing loop with pre_get_posts. This way you can be sure that all extra query parameters will be taken into account.

An example of how you would do this:

add_action( 'pre_get_posts', 'wpse5477_pre_get_posts' );
function wpse5477_pre_get_posts( $query )
{
    if ( ! $query->is_main_query() || $query->is_admin() )
        return false; 

    if ( $query->is_category() ) {
        $query->set( 'post_type', 'video' );
        $query->set( 'posts_per_page', 6 );
    }
    return $query;
}

This code will go in your functions.php.

First we check if this is the main loop and not your admin area. pre_get_posts can affect admin.

Then, if this is a category, we modify the $query.

And then return the $query.

For more information check here: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

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.