permalinks - Wrong generated page_nameslug on first publish of custom post type

I have set up a custom post type (CPT) with the support for editor removed:register_post_type( 'review', arra

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.

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.

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Nov 27, 2019 at 18:53 PhilipPhilip 1115 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/reference/functions/flush_rewrite_rules/

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744934722a4601949.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信