1

I want to create some specific url structure for my website. My Pages will be Inner Home pages that will combine articles and reviews. Articles and reviews are 2 separate custom post types. I would not like to make them child pages for the Inner Home page and have all the pages in one Pages list. I will create Article CPT and add ACF Field to connect article to some Inner Page So, I want the CPT URL to look like a child page of some Page (basic post type - 'page'): website.com/%inner_home_page_slug%/%article_slug%

%inner_home_page_slug% - related (by acf field) page slug

%article_slug% - usual post slug

/article/ cpt base should be removed

Is it possible? (I am aware of the slug conflict, so probably it's ok to leave cpt slug and have structure like %inner_home_page_slug%/article/%article_slug%)

Can anyone help me? I will be eternally grateful

2
  • To clarify: You want the parent slug to be dynamic based on the value of a custom field?
    – MarcGuay
    Commented Mar 7 at 21:33
  • @MarcGuay Yes, exactly. I've coped with it! Commented Mar 9 at 14:40

1 Answer 1

0

Resolved!

So I've added rewrite rule:

add_rewrite_rule(
    '^[^\/]*\/review\/([^\/]*)$',
    'index.php?review=$matches[1]',
    'top'
);

And additional code to create appropriate url structure:

function rev_update_slug( $post_link, $post, $leavename ) {

    if ( 'review' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
    //** Field where related page post object stored in ACF field **//
    $pageid= get_field('related_home_inner_page',$post->ID);
    
    $post_link = str_replace( '/review/', '/'.$pageid->post_name.'/review/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'rev_update_slug', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.