• tiptap

    (@tiptap)


    Hey,

    I have several custom post types, but i would like the posts with in them to have the same url structure as the posts post type.

    For example currently I have;

    For Posts: /%post_id%/%postname%/
    For CPTs Posts: /post-type-name/post-name/

    I’m happy with the CPT archive to remain as /post-type-name/ but the posts within it I’d like to replace the post type name with the posts ID ie making the same as posts /%post_id%/%postname%/

    I’m not sure if thats posible but I’m guessing it might be?

    Any help / pointers appreciated

    Cheers

Viewing 1 replies (of 1 total)
  • Thread Starter tiptap

    (@tiptap)

    I think I’m almost there with this now.

    I’ve managed to get the post_id into the url of the custom post types post.

    ie mydomain.com/custom-post-type/post_id/post-name/

    add_filter('post_type_link', 'my_post_type_link', 1, 3);
    				function my_post_type_link( $post_link, $post = 0, $leavename = false ) {
    
    					if ( strpos('%post_id%', $post_link) === 'false' ) {
    						return $post_link;
    					}
    					if ( is_object($post) ) {
    						$post_id = $post->ID;
    					} else {
    						$post_id = $post;
    						$post = get_post($post_id);
    					}
    					if ( !is_object($post) || $post->post_type != 'features' ) {
    						return $post_link;
    					}
    
    					//put post ID in place of %post_id%
    					return str_replace('%post_id%', $post_id, $post_link);
    
    				}

    Then in the bit where I setup the post type, i add the following to the slug

    'slug' => '/feature/%post_id%',

    So all i need to do now I think is write a rewrite to remove the /feature part and it should work ‘hopefully’, but no luck with that yet

Viewing 1 replies (of 1 total)

The topic ‘URL structure for CPT's same as posts’ is closed to new replies.