so i have a bit of a bug on my hands. I have created a cpt with a custom field attached and want to display the field in the url structure. It works great, but i also want to split the single post into two pages using wordpress nextpage tag.

It works great if i dont have the field attached but not with.

So here is my url now , But /post_title/2/ just gives me a 404.

Here is how i set it up.

function custom_permalink($url, $post) {
    if ($post->post_type == 'test' ) {
        global $post;

        $post_type = $post->post_type;

        $custom_field = get_field('category_in_url', $post->ID); // ACF

        $url = str_replace( $post_type . '/', $post_type . '/' . $custom_field . '/' , $url);
    return $url;
add_filter('post_type_link', 'custom_permalink', 10, 2);

function custom_rewrite_rule() {
    global $wp_rewrite;

    add_rewrite_rule('^test/(.*)/(.*)/(.*)/?', 'index.php?test=$matches[3]', 'top');
    add_rewrite_rule('^test/(.*)/(.*)/?', 'index.php?test=$matches[2]', 'top');


add_action( 'init', 'custom_rewrite_rule', 1);

I have tried adding all sorts of stuff in the rewrite urls, but cant get it to work :(

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.