Change 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/

Screenshot of the Admin panel

Screenshot of the site

add_action( ‘init’, ‘subtitle_post_type’ );

function subtitle_post_type()
{

$labels = array(
‘name’ => ‘Subtitles’,
// more labels goes here
);
$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’ ),
// more labels goes here
);

$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:: Change custom post type permalink according to category

Leave a Reply

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