1

I have set up a custom post type (CPT) with the support for editor removed:

register_post_type( 'review', array(
    'label' => 'Reviews',
    'labels' => array(
        'name' => 'Reviews',
        /* etc */
    ),
    'description' => 'Tour reviews',
    'menu_icon' => 'dashicons-format-chat',
    'public' => true,
    'supports' => array(
        'title',
        'revisions',
        'author',
    ),
    'has_archive' => false,
    'show_in_rest' => true,
    'rewrite' => array('slug' => 'reviews'),
));

(Yes, I'm using only the title for content ;) )

This works fine! However...

When I add a new post of this type, the auto-generated permalink has the slug of another post type's title instead of using the added post type's title.

So it produces:

/reviews/title-of-the-last-post-of-another-post-type/

instead of:

/reviews/newly-added-post-of-this-post-type/

Strangely, the "reviews" part is correct, but the page_name part isn't.

Is this a bug or am I missing something?

NB: If I add support for editor in the register_post_type call, this problem doesn't occur. But I don't want the editor enabled for this post type.

NB2: After first publish, if I manually empty the permalink's edit field and click "Update", the correct slug/page_name is generated.

2 Answers 2

1

When registering or updating any rewrite for any post/custom post type you need to flush rewrite rules by the admin panel or via code:

register_activation_hook( __FILE__, 'plugin_activation' );
function plugin_activation() {
    update_option('plugin_permalinks_flushed', 0);
}

add_action( 'init', 'register_custom_post_type' );
function register_custom_post_type() {
    global $wp;
    register_post_type( 'review', array(
        'label' => 'Reviews',
        'labels' => array(
        'name' => 'Reviews',
            /* etc */
        ),
        'description' => 'Tour reviews',
        'menu_icon' => 'dashicons-format-chat',
        'public' => true,
        'supports' => array(
            'title',
            'revisions',
            'author',
        ),
        'has_archive' => false,
        'show_in_rest' => true,
        'rewrite' => array('slug' => 'reviews'),
    ));

    if( !get_option('plugin_permalinks_flushed') ) {

        flush_rewrite_rules(false);
        update_option('plugin_permalinks_flushed', 1);

    }
}

https://developer.wordpress.org/reference/functions/flush_rewrite_rules/

4
  • Thanks for your suggestion. I tried it, but unfortunately the problem is still there. The generated permalink works correctly, the only problem is the generated slug is not based on the added item's title.
    – Philip
    Nov 27, 2019 at 19:41
  • 1
    I tried to reproduce your error, but when i register this new custom post type and reload permalinks in admin and go to add new reviews post, the slug is generated following the post title. Verify if you have another plugin. Try this custom post type in clean installation of recent worpress. Dec 5, 2019 at 22:05
  • Thank you for testing. I'm using as little plugins as possible. For some extra context: it's part of my custom theme in the latest WP version. Other CPTs work great (right slug too), but this one won't :) Anyway, I'll try to test this in a clean install
    – Philip
    Dec 5, 2019 at 22:11
  • You're right ! Thanks for the help. I placed a update/explanation and workaround as the answer.
    – Philip
    Dec 5, 2019 at 23:02
0

Update

As Vitor Almeida suggested, I tried this in a blank installation of WP (v5.3) with only the default plugins installed and my theme. In this setting all works fine, as it should. Seems like one of the plugins below messes the slug up by providing the slug input field with a default (wrong) value when creating a new post, instead of leaving it blank:

  • Yoast SEO Premium
  • WP Rocket
  • ShortPixel Image Optimizer
  • Polylang
  • Contact Form 7
  • GDPR Cookie Consent
  • Instagram Feed Pro Developer

Workaround / 'Solution'

I used a inline jQuery script to empty the slug input field on load and before publishing/updating, to make sure the WP script handling the post is forced to generate a slug based on the title. A little messy, but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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