0

I would like to know why my custom post type pages do not accept paged pages?

On a normal page I can list custom post types without problems, but on a custom post type page, it seems impossible. I would like to make custom list pages to list custom posts types based on various queries.

I.e. if I would like to list all my custom post types on a page with a pagination, the URL or permalink structure would look something like this: http://www.url.com/taxonomy/document/page/2/, but I can't access that page at all, it simply redirect the additional pages (like 2, 3, 4, etc) to the root page http://www.url.com/taxonomy/document/.

I tried all kind of weird and wonderful solutions that I've found online, but nothing seems to fix it?

The reason I don't want to use a standard page with a custom template, is because it will need specific settings to set up search queries, which a normal page should not have.

This is the code snippet, and it works totally fine on a custom page template (template-list.php), but not on a custom post type page.

$entry = new ThemeOptions\PropertyListing();

$max = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(

    'post_type'         => 'property',
    'posts_per_page'    => $max,
    'paged'             => $paged,
    'tax_query'         => '',
    'meta_query'        => '',
    'post_status'       => 'publish',
    'orderby'           => '',
    'order'             => 'DESC'
);

$query = new WP_Query($args);

if ($query->have_posts()) :

    while($query->have_posts()) : $query->the_post();

        echo $entry->display($post);

    endwhile;

    $wp_query = $query;

    get_template_part('/includes/layout/pagination', '');

else:

    echo "No posts found!! <br>";

endif;

This is the custom post type on which I would like to list other post types and on which the pagination or paged pages fails.

[slug] => listing
[label] => Listing
[labels] => Array
    (
        [name] => Listings
        [singular_name] => Listing
        [menu_name] => Listings
        [all_items] => Listings
        [add_new] => Add New
        [add_new_item] => Add New Listing
        [edit] => Edit
        [edit_item] => Edit Listing
        [new_item] => New Listing
        [view] => View Listing
        [view_item] => View Listing
        [search_items] => Search Listings
        [not_found] => No Listings found
        [not_found_in_trash] => No Listings found in Trash
        [parent_item_colon] => Parent Listing
    )

[description] => Listing page is a custom page type to store and display a search listing page.
[public] => 1
[exclude_from_search] => 1
[publicly_queryable] => 1
[show_ui] => 1
[show_in_nav_menus] => 1
[show_in_menu] => 1
[show_in_admin_bar] => 1
[menu_position] => 21
[menu_icon] => dashicons-admin-page
[capability_type] => listing
[capabilities] => Array
    (
        [read] => read
        [edit_post] => edit_listing
        [edit_posts] => edit_listings
        [edit_others_posts] => edit_others_listings
        [edit_private_posts] => edit_private_listings
        [edit_published_posts] => edit_published_listings
        [publish_posts] => publish_listings
        [read_post] => read_listing
        [read_private_posts] => read_private_listings
        [delete_post] => delete_listing
        [delete_posts] => delete_listings
        [delete_others_posts] => delete_others_listings
        [delete_private_posts] => delete_private_listings
        [delete_published_posts] => delete_published_listings
    )

[map_meta_cap] => 1
[hierarchical] => 
[register_meta_box_cb] => 
[has_archive] => 
[rewrite] => Array
    (
        [slug] => listing
    )

[query_var] => 1
[can_export] => 1
[supports] => Array
    (
        [0] => title
        [1] => editor
        [2] => author
        [3] => thumbnail
        [4] => revisions
        [5] => page-attributes
    )

I'm not sure if there is anything missing to allow pagination

9
  • How are you registering your post type? Also, what is the posts_per_page option set to - do you have enough posts in the database to require pagination?
    – Welcher
    Commented Oct 29, 2016 at 15:37
  • Quick note: You don't need to use the $max variable, WP_Query will use what that option is set to by default :)
    – Welcher
    Commented Oct 29, 2016 at 15:37
  • Whether or not a page exists is based on the results of the main query. If the main query doesn't contain posts, your template will not load and your own query will never run. This is why trying to paginate additional queries on any sort of archive is fraught with disaster. Your best course of action is to use the main query, and alter query parameters via pre_get_posts, so you are affecting the main query before it runs. Then you can use the standard loop in your template and remove the extra query.
    – Milo
    Commented Oct 29, 2016 at 17:14
  • Yes the post type is registered and I've created a page where this code snippet is running. And yes there are enough posts for the pagination to work, i.e. I've set it to two posts per page under the reading settings. The pagination is generated as it should and in this case, it shows 4 pages, but I can't access any of those pages when I click on the pagination links. It redirect the page to the root page.
    – Abstractic
    Commented Oct 30, 2016 at 3:53
  • For your information, this code snippet and pagination works perfectly fine on a normal page, or a page template (template-list.php), but not on a custom post type page?
    – Abstractic
    Commented Oct 30, 2016 at 3:55

1 Answer 1

1

It seems like you need to override the canonical redirection on custom post types to get the pagination to work. It had nothing to do with the custom query used and described above, as that worked perfectly fine on a normal page and now also on a custom post type.

I figured out that I could use the following in my functions file:

/**
* Prevents custom post type pages from being redirected on pagination
*
* @param $url   - root URL
*/

if (! function_exists('disable_redirect_canonical') ) :

    function disable_redirect_canonical($url) {

        $accepted = array('listing');

        if (is_paged() && is_singular() && in_array(get_post_type(), $accepted)) { $url = false; }

    return $url; 
}
endif;
add_filter('redirect_canonical', 'disable_redirect_canonical');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.