register_post_type()’s rewrite slug not working

I’m creating a theme for personal use and in wp-content/mu-plugins/custom-types.php, i set

function artwork_post_types()
{
    register_post_type('artwork', array(
        'public' => true,
        'menu_icon' => 'dashicons-art',
        'labels' => array(
            'name' => 'Artwork',
            'add_new_item' => 'Add New Artwork',
            'edit_item' => 'Edit Artwork',
            'all_items' => 'All Artwork',
            'singular_name' => 'Artwork',
            'pages' => true,
            'rewrite' => array(
                'slug' => 'art',
                'with_front' => false,
            ))
    ));
 }

add_action('init', 'artwork_post_types');

After that, I went to click on Settings -> Permalinks -> Save Changes to update. However it seems the slug rewriting is not working as i hoped.

The permalink still reflects as ‘artwork’ instead of ‘art’
i.e. http://domain/artwork/test instead of http://domain/art/test instead

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The problem is because your rewrite argument is part of the labels, but rewrite is actually same level as the labels, so just move the rewrite to after the labels and the problem will be gone. 🙂

register_post_type('artwork', array(
    'public'    => true,
    'menu_icon' => 'dashicons-art',
    'labels'    => array(
        'name'          => 'Artwork',
        'add_new_item'  => 'Add New Artwork',
        'edit_item'     => 'Edit Artwork',
        'all_items'     => 'All Artwork',
        'singular_name' => 'Artwork',
    ),
    // Move the "rewrite" to below:
    'rewrite'   => array(
        'slug'       => 'art',
        'with_front' => false,
        'pages'      => true, // this also belongs in "rewrite" and not "labels"
    ),
));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x