How to list custom post types on a custom post type page?

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

Topic paged custom-taxonomy pagination custom-post-types Wordpress

Category Web


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');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.