Hossein Haji Mali
Hossein Haji Mali

Reputation: 339

wordpress pagination page not found on page 2

i have a custom post type and here is my functions.php:

function create_blog_post_type()
{
    register_post_type('blog',
        array(
            'menu_icon' => 'dashicons-format-aside',
            'labels' => array(
                'name' => __('Blog'),
                'singular_name' => __('Blog'),
                'menu_name' => "Blog",
                'name_admin_bar' => "Blog",
                'add_new' => "Add New Post",
                'not_found' => "No Post Found.",
                'add_new_item' => "Add New Post",
                'edit_item' => "Edit this Post",
                'view_item' => "View Post",
                'search_items' => "Search Blog",
                'not_found_in_trash' => "No Post Found"
            ),
            'public' => true,
            'rewrite' => array('slug' => 'blog'),
            'supports' => array('title', 'thumbnail', 'editor', 'excerpt', 'comments')
        )
    );
}
add_action('init', 'create_blog_post_type');

I have a blog overview page and it's working fine:

http://www.example.com/blog/

my singular pages are working fine:

http://www.example.com/blog/post_title_here

and I can see my pagination in blog overview page. but when I click on page 2 link:

http://www.example.com/blog/page/2/

here is my php file codes, this is my wp_query:

$queryBlog = new WP_Query(array(
                'post_type' => 'blog',
                'posts_per_page' => 6,
                'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
                'post_status' => 'publish'
            ));

and here is my pagination part:

$total_pages = $queryBlog->max_num_pages;
$big = 999999999;
if ($total_pages > 1) {
    echo paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => max(1, $paged),
        'total' => $total_pages,
        'mid_size' => 1,
        'prev_text' => __('«'),
        'next_text' => __('»'),
        'type' => 'list'
    ));
 }

it goes to 404 (page not found) page, I tried all the answers, but no one worked for me, I don't know what to do.

Upvotes: 0

Views: 101

Answers (1)

Hossein Haji Mali
Hossein Haji Mali

Reputation: 339

Well after checking on my codes, finally i found the problem and the solution.

The problem:
- I had a "blog" custom post type, with "blog" slug.
- I had a "blog" page, with "blog" slug.
the problem was that these 2 had conflict with each other.

The solution:
As i mentioned, i didn't want to change my "blog" post type slug, so easily i changed my www.example.com/blog/ page permalink to something else like www.example.com/blog-overview/, now the pagination works very fine and "page not found" went away.

Upvotes: 1

Related Questions