Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
post type link in 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment