Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Pagination across mutliple custom post types

  • Hi there,
    I have a portfolio archive page that is made up of two different custom post types. I’ve added the URL into the private info.

    The page contains a query loop which queries two different custom post types (WordPress Projects & Shopify Projects) and displays the results in chronological order. This is exactly as I want this page to function.

    When I click into one of the portfolio items, down the bottom I have added a pagination element of whereby I can move back and forwards between projects. However, the pagination only applies to the custom post type I’m currently on. So, if I am looking at a Shopify project item, I can only move forwards and backwards between other Shopify projects.

    This makes sense I guess as the dynamic data is only pulling data from a single custom post type and not both. My question is, can I include both custom post types within the query? Or is there some other way I can create pagination to move between both custom post types in a chronological order.

    See attached screenshot of the current dynamic data query.

  • Hi there,

    This is expected behaviour. In WordPress, previous_post_link() and next_post_link() (and their underlying function get_adjacent_post()) only work within the same custom post type (CPT) by default — they don’t cross post types.

    Give this PHP code a try to include multiple post types in post navigation:

    function include_multiple_cpts_in_navigation( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
        // List of post types you want to include
        $post_types = [ 'cpt1', 'cpt2', 'cpt3' ];
    
        // Convert array into quoted, comma-separated list
        $post_types_in = "'" . implode( "', '", esc_sql( $post_types ) ) . "'";
    
        // Replace the default post_type condition
        $where = preg_replace( "/post_type = '(.*?)'/", "post_type IN ($post_types_in)", $where );
    
        return $where;
    }
    add_filter( 'get_previous_post_where', 'include_multiple_cpts_in_navigation', 10, 5 );
    add_filter( 'get_next_post_where', 'include_multiple_cpts_in_navigation', 10, 5 );
  • Perfect, that works! Thanks very much.

  • Just reopening this with a follow up question.

    I’d like to move this to the top of the page under the header and make it sticky. I’ve moved it to the top ok (thought I haven’t figured why the big gap yet), but is it possible to make it sticky?

    Thanks! See attached image.

  • Hi there,

    Try adding this CSS:

    .single .paging-navigation {
        position: sticky;
        top: 100px;
    }
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.