AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
823 stars 168 forks source link

Multiple URLs and wrong canonical url with ACF custom post type and custom permalink #893

Closed willmoy closed 5 months ago

willmoy commented 5 months ago

To Reproduce Steps to reproduce the behavior:

  1. Create a new custom post type (in my case Post Type Key = 'group')
  2. Under Advanced Settings > URLs, change Permalink Rewrite to 'custom permalink'
  3. In the URL slug box put %group% (the post type key)
  4. Create a new post of that post type
  5. View source, look for
  6. The href in that tag is /%postname%/%postname%/

Expected behavior /%postname%/ should return 200 The canonical URL should be /%postname%/ The URL /%posttypekey%/%postname%/ should return 404

The canonical URL is correct when Permalink Rewrite is set to Post Type Key, so this seems to be a problem with the custom permalink.

Changing the Front URL Prefix setting did not change this.

Code [ { "key": "post_type_659ff3e81e830", "title": "Groups", "menu_order": 0, "active": true, "post_type": "group", "advanced_configuration": true, "import_source": "", "import_date": "", "labels": { "name": "Groups", "singular_name": "Group", "menu_name": "Groups", "all_items": "All Groups", "edit_item": "Edit Groups", "view_item": "View Groups", "view_items": "View Groups", "add_new_item": "Add New Groups", "add_new": "Add New Groups", "new_item": "New Groups", "parent_item_colon": "Parent Groups:", "search_items": "Search Groups", "not_found": "No groups found", "not_found_in_trash": "No groups found in Trash", "archives": "Groups Archives", "attributes": "Groups Attributes", "featured_image": "", "set_featured_image": "", "remove_featured_image": "", "use_featured_image": "", "insert_into_item": "Insert into groups", "uploaded_to_this_item": "Uploaded to this groups", "filter_items_list": "Filter groups list", "filter_by_date": "Filter groups by date", "items_list_navigation": "Groups list navigation", "items_list": "Groups list", "item_published": "Groups published.", "item_published_privately": "Groups published privately.", "item_reverted_to_draft": "Groups reverted to draft.", "item_scheduled": "Groups scheduled.", "item_updated": "Groups updated.", "item_link": "Groups Link", "item_link_description": "A link to a groups." }, "description": "", "public": true, "hierarchical": true, "exclude_from_search": false, "publicly_queryable": true, "show_ui": true, "show_in_menu": true, "admin_menu_parent": "", "show_in_admin_bar": true, "show_in_nav_menus": true, "show_in_rest": true, "rest_base": "", "rest_namespace": "wp\/v2", "rest_controller_class": "WP_REST_Posts_Controller", "menu_position": "", "menu_icon": "", "rename_capabilities": false, "singular_capability_name": "post", "plural_capability_name": "posts", "supports": [ "title", "editor", "thumbnail" ], "taxonomies": "", "has_archive": false, "has_archive_slug": "", "rewrite": { "permalink_rewrite": "custom_permalink", "slug": "%group%", "with_front": "1", "feeds": "0", "pages": "1" }, "query_var": "post_type_key", "query_var_name": "", "can_export": true, "delete_with_user": false, "register_meta_box_cb": "", "enter_title_here": "" } ]

Version Information:

mattgrshaw commented 5 months ago

Hi @willmoy

If I understand this correctly, I'm not sure this is an issue with ACF - I can replicate the same without ACF activated via register_post_type():

function test_register_post_type() {
    $args = array(
        'public' => true,
        'labels' => array(
            'name'          => 'Trees',
            'singular_name' => 'Tree',
            'menu_name'     => 'Trees',
        ),
        'rewrite' => array(
            'slug' => '%tree%',
        ),
    );

    register_post_type( 'tree', $args);
}
add_action( 'init', 'test_register_post_type' );

Creating a new post with the name "Maple" results in the following canonical: https://acf.test/maple/maple/ (200 OK response)

Visiting /%postname%/ results in a 301 redirect to /%postname%/%postname%/.

Please let me know if I've misunderstood this and we'll take another look. Thanks!