-
WebGeek
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()andnext_post_link()(and their underlying functionget_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 ); -
WebGeek
Perfect, that works! Thanks very much.
-
WebGeek
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.
-
Alvind
Hi there,
Try adding this CSS:
.single .paging-navigation { position: sticky; top: 100px; }
- You must be logged in to reply to this topic.