POST_TYPE_LINK在古登堡不起作用

时间:2020-02-19 作者:Corey Ellis

关于使用post\\u type\\u链接过滤器,有很多答案。gist中的代码运行良好,但有一个例外。。。古腾堡。如果我在讨论中的帖子类型上禁用gutenberg,那么代码就可以工作。或者,如果我从“快速编辑”中保存,则代码可以工作。但如果gutenberg处于活动状态,则客户端、日期或类型将保存/添加到永久链接中

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.com/zzramesses/63d569248b65bf553acb09a3ae1536af

1 个回复
SO网友:Vitauts Stočka

这不适用于ACF或任何其他“旧式”元字段。古腾堡分两步保存帖子。首先,它使用RESTAPI保存所有“新”字段,即在块或Gutenberg边栏中创建的字段。然后创建另一个POST请求,以保存在元框中创建的所有“旧式”字段。即使在侧栏中直观地显示字段,ACF也属于第二类。

永久链接的问题在于它们是创建的(并且post_type_link 当元字段值还不可用时,在REST API步骤中激发函数。您可以尝试添加一些save_post 在第二步(保存元字段值后)钩住以更新permalink,但在重新打开帖子进行编辑之前,您将在管理UI中错误显示permalink。正确的方法是create Gutenberg sidebar 用于永久链接的字段。这些字段是使用REST API保存的,在post_type_link 电话(通过get_post_meta).

相关推荐

Problem with permalinks

我已经更改了类别的基本名称,现在它是“博客”,工作正常。但当我通过/blog/%category%/%postname%/更改结构时。显示404。如果我删除结构中的blog,它会再次工作,但我想使用blog word。问题出在哪里?非常感谢。