4

I would like to rewrite the default post type URL to /blog/2016/11/my-post-name/ without affect the other post type url. I tried:

add_action('admin_menu','remove_default_post_type');
function remove_default_post_type() {
    remove_menu_page('edit.php');
}

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

function set_default_post_type() {
register_post_type( 
    'post',
        array(
        'labels' => array(
            'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
        ),
        'public' => true,
        '_builtin' => false,
        '_edit_link' => 'post.php?post=%d',
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array(
            'slug' => 'blog/%year%/%monthnum%/%postname%/',
            'with_front'=> false,
        ),
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    ) 
 );
}

But with no success. The url stay as /blog/my-post. Is there one way to have permalinks of default post type like /blog/2016/11/my-post-name/ (where 2016 is the year of post and 11 is the month of post) Without affect others post types urls?

  • you can't re-register a built-in post type, post permalink is controlled by Settings > Permalinks page, you can get the format you want there. – Milo Nov 4 '16 at 16:14
  • @Milo I tried this config in admin with blog/%year%/%monthnum%/%postname%/ in admin panel but this affect other custom post type that already exists in the project including yoast breadcrumb trail – WpDev Nov 4 '16 at 16:37
  • custom post types should be registered with with_front set to false so the post permalink structure is not prepended. – Milo Nov 4 '16 at 16:44
  • @Milo as an answer please :) – Rarst Nov 4 '16 at 16:50
  • @Milo you're right.. Please make an answer ;) – WpDev Nov 4 '16 at 17:51
2

Use the field in the admin Settings > Permalinks page to set your permalink structure to /blog/%year%/%monthnum%/%postname%/.

To prevent custom post types from inheriting the post permalink structure, set with_front to false in your register_post_type arguments for all custom post types.

Version 4.4 also added the register_post_type_args filter to allow modification of post type arguments for types registered by code you don't have access to change.

| improve this answer | |
0

You can use Custom Permalink plugin https://ru.wordpress.org/plugins/custom-permalinks/

But it can't use permalink "templates". You will need to enter your custom permalink for each post on editing page.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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