admin管理员组

文章数量:1391947

There are tons of answers on using the post_type_link filter. The code in the gist works perfectly with one exception...gutenberg. If I disable gutenberg on the post types in question then the code works. Or if I save from the quick edit the code works. But if gutenberg is active then the client, date nor type get saved/added to the permalink

add_action( 'init', $n( 'post_types' ) );
add_action( 'init', $n( 'rewrite_tags' ) );
add_filter( 'post_type_link', $n( 'practice_post_type_link' ), 10, 3 );
add_filter( 'post_type_link', $n( 'prescription_post_type_link' ), 10, 3 );

function post_types() {
    $labels = array(
        'name'  => _x( 'Prescription', 'Post Type General Name', 'ws' ),
        'singular_name'  => _x( 'Prescription', 'Post Type Singular Name', 'ws' ),
        'menu_name'  => __( 'Prescriptions', 'ws' ),
        'name_admin_bar' => __( 'Prescription', 'ws' ),
        'archives' => __( 'Prescriptions', 'ws' ),
        'attributes' => __( 'Prescription Attributes', 'ws' ),
        'parent_item_colon' => __( 'Parent Prescription:', 'ws' ),
        'all_items' => __( 'All Prescriptions', 'ws' ),
        'add_new_item' => __( 'Add New Prescription', 'ws' ),
        'add_new' => __( 'Add New', 'ws' ),
        'new_item' => __( 'New Prescription', 'ws' ),
        'edit_item' => __( 'Edit Prescription', 'ws' ),
        'update_item' => __( 'Update Prescription', 'ws' ),
        'view_item' => __( 'View Prescription', 'ws' ),
        'view_items' => __( 'View Prescriptions', 'ws' ),
        'search_items' => __( 'Search Prescription', 'ws' ),
        'not_found' => __( 'Not found', 'ws' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'ws' ),
        'featured_image' => __( 'Featured Image', 'ws' ),
        'set_featured_image' => __( 'Set featured image', 'ws' ),
        'remove_featured_image' => __( 'Remove featured image', 'ws' ),
        'use_featured_image' => __( 'Use as featured image', 'ws' ),
        'insert_into_item '=> __( 'Insert into prescription', 'ws' ),
        'uploaded_to_this_item' => __( 'Uploaded to this prescription', 'ws' ),
        'items_list' => __( 'Prescriptions list', 'ws' ),
        'items_list_navigation' => __( 'Prescriptions list navigation', 'ws' ),
        'filter_items_list' => __( 'Filter Prescriptions list', 'ws' ),
    );
    $args = array(
        'label' => __( 'Prescription', 'ws' ),
        'description' => __( 'Prescription Post Type', 'ws' ),
        'labels' => $labels,
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'hierarchical' => false,
        'public' => false,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 28,
        'menu_icon' => 'dashicons-art',
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => false,
        'can_export' => true,
        'has_archive' => false,
        'rewrite' => array( "slug" => "prescription/%type%", "with_front" => false ),
        'exclude_from_search'=> false,
        'publicly_queryable' => true,
        'capability_type'=> 'post',
        'show_in_rest'=> true,
    );
    register_post_type( 'prescription', $args );

    $labels = array(
        'name'  => _x( 'Practice', 'Post Type General Name', 'ws' ),
        'singular_name'  => _x( 'Practice', 'Post Type Singular Name', 'ws' ),
        'menu_name'  => __( 'Practices', 'ws' ),
        'name_admin_bar' => __( 'Practice', 'ws' ),
        'archives' => __( 'Practices', 'ws' ),
        'attributes' => __( 'Practice Attributes', 'ws' ),
        'parent_item_colon' => __( 'Parent Practice:', 'ws' ),
        'all_items' => __( 'All Practices', 'ws' ),
        'add_new_item' => __( 'Add New Practice', 'ws' ),
        'add_new' => __( 'Add New', 'ws' ),
        'new_item' => __( 'New Practice', 'ws' ),
        'edit_item' => __( 'Edit Practice', 'ws' ),
        'update_item' => __( 'Update Practice', 'ws' ),
        'view_item' => __( 'View Practice', 'ws' ),
        'view_items' => __( 'View Practices', 'ws' ),
        'search_items' => __( 'Search Practice', 'ws' ),
        'not_found' => __( 'Not found', 'ws' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'ws' ),
        'featured_image' => __( 'Featured Image', 'ws' ),
        'set_featured_image' => __( 'Set featured image', 'ws' ),
        'remove_featured_image' => __( 'Remove featured image', 'ws' ),
        'use_featured_image' => __( 'Use as featured image', 'ws' ),
        'insert_into_item '=> __( 'Insert into practice', 'ws' ),
        'uploaded_to_this_item' => __( 'Uploaded to this practice', 'ws' ),
        'items_list' => __( 'Practices list', 'ws' ),
        'items_list_navigation' => __( 'Practices list navigation', 'ws' ),
        'filter_items_list' => __( 'Filter Practices list', 'ws' ),
    );
    $args = array(
        'label' => __( 'Practice', 'ws' ),
        'description' => __( 'Practice Post Type', 'ws' ),
        'labels' => $labels,
        'supports' => array( 'title', 'editor' ),
        'hierarchical' => false,
        'public' => false,
        'show_ui' => true,
        'show_in_menu' => 'edit.php?post_type=client',
        'menu_position' => 29,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => false,
        'can_export' => true,
        'has_archive' => false,
        'rewrite' => array( "slug" => "practice/%client%/%date%", "with_front" => false ),
        'exclude_from_search'=> false,
        'publicly_queryable' => true,
        'capability_type'=> 'post',
        'show_in_rest'=> true,
    );
    register_post_type( 'practice', $args );
}

function rewrite_tags() {
    add_rewrite_tag( '%client%', '([^&]+)', 'client=' );
    add_rewrite_tag( '%date%', '([^&]+)', 'date=' );
    add_rewrite_rule( '^practice/([^/]*)/([^/]*)/?','index.php?client=$matches[1]&date=$matches[2]&name=$matches[3]','top' );

    add_rewrite_tag( '%type%', '([^&]+)', 'type=');
    add_rewrite_rule( '^practice/([^/]*)/([^/]*)/?','index.php?client=$matches[1]&date=$matches[2]&name=$matches[3]','top' );
}

function practice_post_type_link( $permalink, $post, $leavename ) {
    if( 'practice' === $post->post_type ) {
        $date = empty( get_field( 'date', $post->ID ) ) ? get_the_modified_date( 'Y-m-d', $post ) : get_field( 'date', $post->ID );
        $clients = get_field( 'client', $post->ID );
        $client_post = reset( $clients );
        $client = $client_post->post_name;
        if( isset( $date ) && isset( $client ) ) {
            $permalink = home_url( "practice/" . $client . "/" . $date . "/" . $post->post_name . "/" );
        }
    }

    return $permalink;
}

function prescription_post_type_link( $permalink, $post, $leavename ) {
    if( 'prescription' === $post->post_type ) {
        $prescription_type = get_field( 'prescription_type', $post->ID );
        if( !empty( $prescription_type ) ) {
            $type = $prescription_type->slug;
            $permalink = home_url( "prescription/" . $type . "/" . $post->post_name . "/" );
        }
    }

    return $permalink;
}

There are tons of answers on using the post_type_link filter. The code in the gist works perfectly with one exception...gutenberg. If I disable gutenberg on the post types in question then the code works. Or if I save from the quick edit the code works. But if gutenberg is active then the client, date nor type get saved/added to the permalink

add_action( 'init', $n( 'post_types' ) );
add_action( 'init', $n( 'rewrite_tags' ) );
add_filter( 'post_type_link', $n( 'practice_post_type_link' ), 10, 3 );
add_filter( 'post_type_link', $n( 'prescription_post_type_link' ), 10, 3 );

function post_types() {
    $labels = array(
        'name'  => _x( 'Prescription', 'Post Type General Name', 'ws' ),
        'singular_name'  => _x( 'Prescription', 'Post Type Singular Name', 'ws' ),
        'menu_name'  => __( 'Prescriptions', 'ws' ),
        'name_admin_bar' => __( 'Prescription', 'ws' ),
        'archives' => __( 'Prescriptions', 'ws' ),
        'attributes' => __( 'Prescription Attributes', 'ws' ),
        'parent_item_colon' => __( 'Parent Prescription:', 'ws' ),
        'all_items' => __( 'All Prescriptions', 'ws' ),
        'add_new_item' => __( 'Add New Prescription', 'ws' ),
        'add_new' => __( 'Add New', 'ws' ),
        'new_item' => __( 'New Prescription', 'ws' ),
        'edit_item' => __( 'Edit Prescription', 'ws' ),
        'update_item' => __( 'Update Prescription', 'ws' ),
        'view_item' => __( 'View Prescription', 'ws' ),
        'view_items' => __( 'View Prescriptions', 'ws' ),
        'search_items' => __( 'Search Prescription', 'ws' ),
        'not_found' => __( 'Not found', 'ws' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'ws' ),
        'featured_image' => __( 'Featured Image', 'ws' ),
        'set_featured_image' => __( 'Set featured image', 'ws' ),
        'remove_featured_image' => __( 'Remove featured image', 'ws' ),
        'use_featured_image' => __( 'Use as featured image', 'ws' ),
        'insert_into_item '=> __( 'Insert into prescription', 'ws' ),
        'uploaded_to_this_item' => __( 'Uploaded to this prescription', 'ws' ),
        'items_list' => __( 'Prescriptions list', 'ws' ),
        'items_list_navigation' => __( 'Prescriptions list navigation', 'ws' ),
        'filter_items_list' => __( 'Filter Prescriptions list', 'ws' ),
    );
    $args = array(
        'label' => __( 'Prescription', 'ws' ),
        'description' => __( 'Prescription Post Type', 'ws' ),
        'labels' => $labels,
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'hierarchical' => false,
        'public' => false,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 28,
        'menu_icon' => 'dashicons-art',
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => false,
        'can_export' => true,
        'has_archive' => false,
        'rewrite' => array( "slug" => "prescription/%type%", "with_front" => false ),
        'exclude_from_search'=> false,
        'publicly_queryable' => true,
        'capability_type'=> 'post',
        'show_in_rest'=> true,
    );
    register_post_type( 'prescription', $args );

    $labels = array(
        'name'  => _x( 'Practice', 'Post Type General Name', 'ws' ),
        'singular_name'  => _x( 'Practice', 'Post Type Singular Name', 'ws' ),
        'menu_name'  => __( 'Practices', 'ws' ),
        'name_admin_bar' => __( 'Practice', 'ws' ),
        'archives' => __( 'Practices', 'ws' ),
        'attributes' => __( 'Practice Attributes', 'ws' ),
        'parent_item_colon' => __( 'Parent Practice:', 'ws' ),
        'all_items' => __( 'All Practices', 'ws' ),
        'add_new_item' => __( 'Add New Practice', 'ws' ),
        'add_new' => __( 'Add New', 'ws' ),
        'new_item' => __( 'New Practice', 'ws' ),
        'edit_item' => __( 'Edit Practice', 'ws' ),
        'update_item' => __( 'Update Practice', 'ws' ),
        'view_item' => __( 'View Practice', 'ws' ),
        'view_items' => __( 'View Practices', 'ws' ),
        'search_items' => __( 'Search Practice', 'ws' ),
        'not_found' => __( 'Not found', 'ws' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'ws' ),
        'featured_image' => __( 'Featured Image', 'ws' ),
        'set_featured_image' => __( 'Set featured image', 'ws' ),
        'remove_featured_image' => __( 'Remove featured image', 'ws' ),
        'use_featured_image' => __( 'Use as featured image', 'ws' ),
        'insert_into_item '=> __( 'Insert into practice', 'ws' ),
        'uploaded_to_this_item' => __( 'Uploaded to this practice', 'ws' ),
        'items_list' => __( 'Practices list', 'ws' ),
        'items_list_navigation' => __( 'Practices list navigation', 'ws' ),
        'filter_items_list' => __( 'Filter Practices list', 'ws' ),
    );
    $args = array(
        'label' => __( 'Practice', 'ws' ),
        'description' => __( 'Practice Post Type', 'ws' ),
        'labels' => $labels,
        'supports' => array( 'title', 'editor' ),
        'hierarchical' => false,
        'public' => false,
        'show_ui' => true,
        'show_in_menu' => 'edit.php?post_type=client',
        'menu_position' => 29,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => false,
        'can_export' => true,
        'has_archive' => false,
        'rewrite' => array( "slug" => "practice/%client%/%date%", "with_front" => false ),
        'exclude_from_search'=> false,
        'publicly_queryable' => true,
        'capability_type'=> 'post',
        'show_in_rest'=> true,
    );
    register_post_type( 'practice', $args );
}

function rewrite_tags() {
    add_rewrite_tag( '%client%', '([^&]+)', 'client=' );
    add_rewrite_tag( '%date%', '([^&]+)', 'date=' );
    add_rewrite_rule( '^practice/([^/]*)/([^/]*)/?','index.php?client=$matches[1]&date=$matches[2]&name=$matches[3]','top' );

    add_rewrite_tag( '%type%', '([^&]+)', 'type=');
    add_rewrite_rule( '^practice/([^/]*)/([^/]*)/?','index.php?client=$matches[1]&date=$matches[2]&name=$matches[3]','top' );
}

function practice_post_type_link( $permalink, $post, $leavename ) {
    if( 'practice' === $post->post_type ) {
        $date = empty( get_field( 'date', $post->ID ) ) ? get_the_modified_date( 'Y-m-d', $post ) : get_field( 'date', $post->ID );
        $clients = get_field( 'client', $post->ID );
        $client_post = reset( $clients );
        $client = $client_post->post_name;
        if( isset( $date ) && isset( $client ) ) {
            $permalink = home_url( "practice/" . $client . "/" . $date . "/" . $post->post_name . "/" );
        }
    }

    return $permalink;
}

function prescription_post_type_link( $permalink, $post, $leavename ) {
    if( 'prescription' === $post->post_type ) {
        $prescription_type = get_field( 'prescription_type', $post->ID );
        if( !empty( $prescription_type ) ) {
            $type = $prescription_type->slug;
            $permalink = home_url( "prescription/" . $type . "/" . $post->post_name . "/" );
        }
    }

    return $permalink;
}

https://gist.github/zzramesses/63d569248b65bf553acb09a3ae1536af

Share Improve this question edited Feb 19, 2020 at 4:12 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Feb 19, 2020 at 3:54 Corey EllisCorey Ellis 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

This will not work with ACF or any other "old style" meta fields. Gutenberg saves posts in two steps. First, it uses REST API to save all "new" fields, i.e., fields created in blocks or Gutenberg sidebars. Then it creates another POST request to save all "old style" fields created in meta boxes. ACF falls into second category even if field is visually displayed in sidebar.

The problem with permalinks is that they are created (and your post_type_link functions are fired) during REST API step, when meta field values are not yet available. You may try to add some save_post hook to update permalink during second step (after meta field values are saved), but then you'll have permalink incorrectly displayed in admin UI until you re-open post for editing. Proper way would be to create Gutenberg sidebar for fields that are used for permalinks. Those fields are saved using REST API and are available in post_type_link calls (via get_post_meta).

本文标签: custom post typesposttypelink not working in gutenberg