I want to change my custom post type permalink according to category

Actual:http://localhost/wp/subtitle/title-shahbaz/
Post type: subtitle
Category of this post: internal
Want to show everywhere and also in URL bar:http://localhost/wp/internal-subtitle/title-shahbaz/

Screen Shot WP ADMIN
http://prntscr.com/ing7ei
WP SITE FRONT
http://prntscr.com/ing7z7

add_action( ‘init’, ‘subtitle_post_type’ );

function subtitle_post_type()
{

$labels = array(
‘name’ => ‘Subtitles’,
‘singular_name’ => ‘Subtitle’,
‘menu_name’ => ‘Subtitle’,
‘name_admin_bar’ => ‘Subtitle’,
‘archives’ => ‘Item Archives’,
‘parent_item_colon’ => ‘Parent Subtitle:’,
‘all_items’ => ‘All Subtitle’,
‘add_new_item’ => ‘Add New Subtitle’,
‘add_new’ => ‘Add Subtitle’,
‘new_item’ => ‘New Subtitle’,
‘edit_item’ => ‘Edit Subtitle’,
‘update_item’ => ‘Update Subtitle’,
‘view_item’ => ‘View Subtitle’,
‘search_items’ => ‘Search Subtitle’,
‘not_found’ => ‘Not found’,
‘not_found_in_trash’ => ‘Not found in Trash’,
‘featured_image’ => ‘Featured Image’,
‘set_featured_image’ => ‘Set featured image’,
‘remove_featured_image’ => ‘Remove featured image’,
‘use_featured_image’ => ‘Use as featured image’,
‘insert_into_item’ => ‘Insert into item’,
‘uploaded_to_this_item’ => ‘Uploaded to this item’,
‘items_list’ => ‘Items list’,
‘items_list_navigation’ => ‘Items list navigation’,
‘filter_items_list’ => ‘Filter items list’,
);
$args = array(
‘label’ => ‘subtitle’,
‘labels’ => $labels,
‘supports’ => array(‘comments’),
‘hierarchical’ => false,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘menu_position’ => 5,
‘show_in_admin_bar’ => true,
‘show_in_nav_menus’ => true,
‘can_export’ => true,
‘has_archive’ => false,
‘exclude_from_search’ => false,
‘publicly_queryable’ => true,
‘capability_type’ => ‘page’,
‘taxonomies’ => array( ‘category’ )
);
register_post_type( ‘subtitle’, $args );
}

$labels = array(
‘name’ => __(‘Categories’, ‘taxonomy general name’ ),
‘singular_name’ => __( ‘Category’, ‘taxonomy singular name’ ),
‘search_items’ => __( ‘Search Categories’ ),
‘all_items’ => __( ‘All Categories’ ),
‘parent_item’ => __( ‘Parent Category’ ),
‘parent_item_colon’ => __( ‘Parent Category:’ ),
‘edit_item’ => __( ‘Edit Category’ ),
‘update_item’ => __( ‘Update Category’ ),
‘add_new_item’ => __( ‘Add New Category’ ),
‘new_item_name’ => __( ‘New Category Name’ ),
‘menu_name’ => __( ‘Category’ ),
);

$args = array(
‘hierarchical’ => true,
‘labels’ => $labels,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘type’ ),
);

register_taxonomy( ‘type’, array( ‘subtitle’ ), $args );

This code I trying working correct for wp-admin but not for front side

function append_query_string( $url, $post ,$leavename, $sample ) {
$terms = wp_get_post_terms( $post->ID, ‘type’ );
$post_type= $post->post_type;

if( $terms[0]->name == ‘internal’ && ‘subtitle’ == $post_type ) {
return get_site_url( null, “/{$terms[0]->name}-{$post_type}/{$post->post_name}/” );
}
//}
return $url;
}
add_filter( ‘post_type_link’, ‘append_query_string’, 11, 4 );

Read more here:: I want to change my custom post type permalink according to category

Leave a Reply

Your email address will not be published. Required fields are marked *