permalinks – Adding custom query var to a single post without redirecting to its root slug


I have created a post to posts relationship using ACF. On the single page of one of my CPTs I would like to add a prettified ID to the end of the slug to load different values on the page (/my-archive/my-single-post/ID) and have that ID saved to a query variable called custom_id. I also do not want the page to redirect to the original slug (/my-archive/my-single-post/).

I’ve managed to do the following:

add_action( 'init',  'add_custom_id_rewrite_rules' );
function add_custom_id_rewrite_rules() {
    add_rewrite_rule( '^my-archive/([^/]*)/([0-9]+)?$', 'index.php?post_type=my_post_type&pagename=$matches[1]&custom_id=$matches[2]', 'top' );

add_filter( 'query_vars', 'add_custom_id_query_vars' );
function add_custom_id_query_vars( $query_vars ) {
    $query_vars[] = 'custom_id';
    return $query_vars;

From everywhere I read, this should work. I’ve installed the Rewrite Rule Analyzer and it detects my custom_id correctly. However, despite it being considered correct I still get redirected to /my-archive/my-single-post/.

I want the query to still pull the_title() from that single post I just want to be able to query custom_id to change things on the page.


Gazillion 1 year 2021-09-01T13:11:38-05:00 0 Answers 0 views 0

Leave an answer