-1

Original Url: https://www.something.com/category/ftm-atf/

When I add pagination in, the following url is generated which results in a 404:

Paginated Url: https://www.something.com/category/ftm-atf/page/2/

The code is located in category.php and looks like the following:

<?php
$category = get_category( get_query_var( 'cat' ) );
query_posts(array( 
'post_type' => 'video',
'showposts' => 6,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array($category->term_id)
)
)
));
?>
<?php while (have_posts()) : the_post(); ?> 

...

<?php endwhile;?>

paginate_links(array('total'=> $wp_query->max_num_pages));

I've tried various solutions to no avail, so I feel like something basic is being missed. Essentially, all I need to have happen is to restrict to 6 posts per page, and allow simple pagination.

Thank you so much!

1

if your aim is to restrict the category archive to your post_type 'video' and to 6 posts per page, do not edit category.php, rather use 'pre_get_posts' https://developer.wordpress.org/reference/hooks/pre_get_posts/

example code for your case:

add_action( 'pre_get_posts', 'category_post_type_video_custom' );
function category_post_type_video_custom( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        // Not a query for an admin page.
        // It's the main query for a front end page of your site.
        if ( is_category() ) {
            // It's the main query for a category archive.
            // Let's change the query for category archives.
            $query->set( 'post_type', array( 'video' ) );
            $query->set( 'posts_per_page', 6 );
        }
    }
}

virtually directly taken from https://developer.wordpress.org/reference/hooks/pre_get_posts/#targeting-the-right-query

| improve this answer | |
  • Thank you Michael. Yes, I keep reading about that and figured it had something to do with this function. I didn't develop the site so I'm jumping in and fixing that the previous person has messed up. Going with your solution, I'm assuming I add this to functions.php? Also, what should I do with category.php? – Gene Ellis Mar 12 at 5:36
  • in category.php, remoive everything from before this line <?php while (have_posts()) : the_post(); ?> in your posted code. and consider to add some if( have_posts() ) code to the fiile. – Michael Mar 12 at 16:39
  • Question. Are you saying I should also change while (have_posts()) to if( have_posts()) or I should keep them both? Just trying to ensure the loop stays in tact to display the items. Thanks so much! Really excited to solve this issue! – Gene Ellis Mar 13 at 4:48
  • use both... for a general loop design, see developer.wordpress.org/themes/basics/the-loop – Michael Mar 13 at 19:13
  • Will do. Thanks Michael! – Gene Ellis Mar 15 at 20:42

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.