Search code examples
wordpresspermalinks

redefining the 'POST' post-type slug with register_post_type_args


I am trying to redefine the properties of the default post type 'POST'. I'm trying with the function register_post_type_args.

I want to redefine the label of the post type from "article" to "blog" and i want to add the slug "blog" for this post type in his url (mysite/blog/my-post)

Here is his call in functions.php:

function post_to_blog ($args, $post_type) {
    if ('post' !== $post_type) {
        return $args;
    }

    $args ['label'] = 'Blog';
    $args ['rewrite'] = array ('slug' => "blog", 'with_front' => true);
    return $args;
}
add_filter ('register_post_type_args', 'post_to_blog', 1, 2);

The label property is correctly redefined. The label is changed in the back office. But the post slug is not taken into account. The url of my posts is still mysite/name-of-post and not mysite/blog/name-of-post

(I can't add the slug in settings / permalinks because I need to keep the default url type for other post types).

I think I do it with register_post_type_args but I don't understand why the slug is not taken into account. Do you have an explanation ?

EDIT :

Just out of curiosity, I tried to redefine the post with register_post_type. This is not the correct method, but with this solution, the URLs are correctly redefined.

register_post_type( 'post', array(
    'label' =>  'Blog',
    'rewrite' => array('slug' => "blog/", 'with_front' => true),
));

Solution

  • I come back on this problem because it seems that there is no solution with register_post_type_args.

    In fact, it seems that it is impossible to redefine some default post-type properties. It is possible to modify some such as the Label. But slug is one of the properties that can not be changed.

    For my permalinks problem, I was forced to use the permalinks settings in the back-office, and solve that problem in another way.

    The function register_post_type_args however works perfectly with a custom post type. Which can be completely redefined.

    This may eventually help some.