[resolved] custom post type - paged archives (6 posts)

  1. Jon
    Posted 5 years ago #

    I've got a custom post type ("news") registered and working very well.

    I have created a custom archive template for the post type (archive-news.php), and the initial load ({domain}/news/) is functioning properly. However, trying to access previous posts (i.e. {domain}/news/page/2) results in a 404 error, even though the $query_string variable looks to be correct (paged=2&post_type=news).

    Is there a way to have a paged archive for a custom post type, or has WordPress not made it there yet?

    If it's not yet supported, is there a preferred plugin being used for this purpose?

  2. Jon
    Posted 5 years ago #

    Hmm, so I managed to get this working by trying a hunch.

    Here is my paging code on my archive-news.php template:

    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    global $query_string;
    query_posts( $query_string . "&posts_per_page=3&paged=" . $page);

    I was basically setting the posts per page to 3 for this particular post type only.

    So on a hunch I went into Settings > Reading and changed the "Blog pages show at most" value down to 3, and suddenly paging worked for my custom post type.

    After playing around for a bit, it seems that WordPress is using the value in Settings > Reading on page load - meaning it looks for posts 11-20 for page 2 out of the box. Only after verifying that 20 or more posts exist does it run the custom paging query. I only have 7 news posts so far, so with the default settings, a page 2 did not exist.

    I ended up setting the "Blog pages show at most" down to 1 to ensure that all future attempts at paging custom post types would work, regardless of how many I wanted to show per page.

  3. lancemonotone
    Posted 5 years ago #

    THANK YOU. This has to be a bug.

  4. Harish Chouhan
    Posted 5 years ago #

    Hello Guys,

    I have managed to make my page which displayed list of posttypes with pagination with a sorting order to work when I read this post first time.

    However I have not been able to make my single-work.php (template to show just a single custom posttype) to work using your code.

    With default loop, it shows my custom post type propery, but the sorting order is based on date, and that is how I have been trying to use till now. But I need to sort it with menu_order field and not date.

    If I use below code, then only the latest post is displayed on all pages.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  5. roowilliams
    Posted 4 years ago #

    This isn't resolved, I am in the same situation and cannot set my post limit to 1 in the admin as I have a blog which I want around 10 posts on, even though i only want 3 on my custom post type.

    Was there ever a solution to this?

  6. alanchrishughes
    Posted 4 years ago #

    I have a really bizarre glitch where it goes through each page just fine until it reaches the last page and then it shows, not even my 404 page, but some random broken page.

Topic Closed

This topic has been closed to new replies.

About this Topic