ACF CPT URL to include $post_id

I know this may not be the best place to post this question but I’m wondering if anyone knows how to accomplish this. I have several CPTs and I want one specifically to have the post ID in the URL instead of the slug as they have by default. I see within the ACF interface an option to rewrite the URL but I’m not sure how to do this and insert a variable like that.

Hello Nicholas,

Thanks for writing in! What you are trying to accomplish requires custom PHP coding. You can utilize the post_type_link filter.

For example;

// Retain the URLs of the News Post Type and News Articles
// =============================================================================
function custom_news_post_type_permalink($post_link, $post) {
    if ($post->post_type == 'news') {
        return home_url('news/' . $post->ID . '/');
    }
    return $post_link;
}
add_filter('post_type_link', 'custom_news_post_type_permalink', 10, 2);

function custom_news_post_type_rewrite_rules() {
    add_rewrite_rule('^news/([0-9]+)/?$', 'index.php?post_type=news&p=$matches[1]', 'top');
}
add_action('init', 'custom_news_post_type_rewrite_rules', 100);

The above code will give you https://example.com/news/12345 where {12345} is the ID of the news article item. The posts in your blog will still have the https://example.com/blog/hello-word/

Be advised that custom coding is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may select our One service for further assistance.

Kindly let us know how it goes.