Pagination in Wordpress category returning 404

Asked

Viewed 64 times

0

Hello!

I know this is probably a repeated question, but as none of the solutions I found around here have managed to solve my problem, I decided to do it anyway.

I’m working on a template with a custom post type. To make the pagination, I used this tutorial here https://medium.com/@bikramkc/wordpress-custom-pagination-functions-php-without-a-plugin-961bc4fb930f

It works perfectly on the Archive of my Cpt, but in the categories of the same, when I change page it returns me a 404.

Follows the code:

// archive.php

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
    "post_type" => "clientes",
    "posts_per_page" => '12',
    "paged" => $paged
];
query_posts($args);
if (have_posts()) :
    ?>
<ul class="customers-section--list">
    <?php
        while (have_posts()) : the_post(); ?>
[...]
<?php
    if (function_exists('custom_pagination')) {
        custom_pagination($loop->max_num_pages, "", $paged);
    }
endif;
?>
// category.php

<?php

    $currCat = get_category(get_query_var('cat'));
    $cat_name = $currCat->name;
    $cat_id   = get_cat_ID( $cat_name );

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = [
        "post_type" => "clientes",
        "posts_per_page" => '12',
        "paged" => $paged,
        'category_name' => $cat_name
    ];
    query_posts($args);
    if (have_posts()) :
?>
<ul class="customers-section--list">
    <?php
        while (have_posts()) : the_post();
[...]
<?php
    if (function_exists('custom_pagination')) {
        custom_pagination($loop->max_num_pages, "", $paged);
    }
endif;

Custom post type setup

add_action('init', 'cpt_customers');
function cpt_customers()
{
    $labels = [
        'name' => 'Clientes',
        'singular_name' => 'Cliente'
    ];

    $args = [
        'labels' => $labels,
        'supports' => ['title', 'editor', 'thumbnail'],
        'public' => true,
        'menu_icon'   => 'dashicons-groups',
        'taxonomies' => ['category'],
        'has_archive' => true,
    ];

    register_post_type('clientes', $args);
}

1 answer

1


Paging will not work properly as you are using the function query_posts() which should only be used internally in Wordpress, their documentation makes it clear that it should not be used within themes or plugins, since this function will rewrite the page loop, causing known problems of performance and paging.

See the documentation, have all the details and explaining the reasons not to use more details and also alternatives for implementation: https://developer.wordpress.org/reference/functions/query_posts/

But anyway, in your case the right thing is to use pre_get_posts, so you can modify the loop without forcing it to run again while loading the page.

In addition to registering the post type you want, folders are already created for files and categories, without needing to modify any file, however if you need to change the name, as I said, use pre_get_posts.

  • I did it! I also needed to make some changes to the permalink menu, but it worked here. Thank you! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.