Search code examples
phpwordpresscustom-post-typepermalinks

How to make Wordpress Custom Post Type Permalink


This is simple picture

Permalinks

Post Types

Register Post Types

I'll be using this permalinks settings (e.g. /%second%%minute%%hour%%day%)

Now deafult post link > www."mywebsite".com/12331406

I have a post_type simple: onur

onur's posts working with this permalink > www."mywebsite".com/onur/post-title

But I need > www."mywebsite".com/12331406 or/else > www."mywebsite".com/onur/12331406 not problem.

Any help...?


Solution

  • I have done like this what you want but i don't know whether this will work for you or not

    just add this in functions.php file

    add_filter('post_type_link', 'woorei_33551_custom_listing_permalink', 1, 3);
    
    function woorei_33551_custom_listing_permalink($post_link, $post = 0, $leavename) {
    
        if ( $post->post_type == 'CUSTOM_POST_TYPE' ) {
            //You can whatever you want to instead of post id
            return home_url( '/' . $post->ID);
    
        }
    
        else {
    
            return $post_link;
    
        }
    
    }
    
    add_action( 'init', 'woorei_33551_rewrites_init' );
    
    function woorei_33551_rewrites_init(){
    
        add_rewrite_rule(
    
            '([0-9]+)?$',
    
            'index.php?post_type=CUSTOM_POST_TYPE&p=$matches[1]',
    
            'top' );
    
    }