I’m having an issue with pagination not working while trying to get custom post type permalink. I’ve Custom post type ‘songs’ and custom taxonomy ‘singer’ here:
example.com/songs/
// Custom Post Type
example.com/singer/
// Custom Taxonomy
example.com/songs/article
// Post Permalink
I want it to look like
example.com/singer/article
/// site.com/custom-taxonomy/post
here is my Custom Post Type Code
add_action( 'init', 'your_prefix_register_post_type' );
function your_prefix_register_post_type() {
$labels = [
'name' => esc_html__( 'Songs', 'your-textdomain' ),
'singular_name' => esc_html__( 'Song', 'your-textdomain' ),
'add_new' => esc_html__( 'Add New', 'your-textdomain' ),
'add_new_item' => esc_html__( 'Add new song', 'your-textdomain' ),
'edit_item' => esc_html__( 'Edit Song', 'your-textdomain' ),
'new_item' => esc_html__( 'New Song', 'your-textdomain' ),
'view_item' => esc_html__( 'View Song', 'your-textdomain' ),
'view_items' => esc_html__( 'View Songs', 'your-textdomain' ),
'search_items' => esc_html__( 'Search Songs', 'your-textdomain' ),
'not_found' => esc_html__( 'No songs found', 'your-textdomain' ),
'not_found_in_trash' => esc_html__( 'No songs found in Trash', 'your-textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Song:', 'your-textdomain' ),
'all_items' => esc_html__( 'All Songs', 'your-textdomain' ),
'archives' => esc_html__( 'Song Archives', 'your-textdomain' ),
'attributes' => esc_html__( 'Song Attributes', 'your-textdomain' ),
'insert_into_item' => esc_html__( 'Insert into song', 'your-textdomain' ),
'uploaded_to_this_item' => esc_html__( 'Uploaded to this song', 'your-textdomain' ),
'featured_image' => esc_html__( 'Featured image', 'your-textdomain' ),
'set_featured_image' => esc_html__( 'Set featured image', 'your-textdomain' ),
'remove_featured_image' => esc_html__( 'Remove featured image', 'your-textdomain' ),
'use_featured_image' => esc_html__( 'Use as featured image', 'your-textdomain' ),
'menu_name' => esc_html__( 'Songs', 'your-textdomain' ),
'filter_items_list' => esc_html__( 'Filter songs list', 'your-textdomain' ),
'filter_by_date' => esc_html__( '', 'your-textdomain' ),
'items_list_navigation' => esc_html__( 'Songs list navigation', 'your-textdomain' ),
'items_list' => esc_html__( 'Songs list', 'your-textdomain' ),
'item_published' => esc_html__( 'Song published', 'your-textdomain' ),
'item_published_privately' => esc_html__( 'Song published privately', 'your-textdomain' ),
'item_reverted_to_draft' => esc_html__( 'Song reverted to draft', 'your-textdomain' ),
'item_scheduled' => esc_html__( 'Song scheduled', 'your-textdomain' ),
'item_updated' => esc_html__( 'Song updated', 'your-textdomain' ),
];
$args = [
'label' => esc_html__( 'Songs', 'your-textdomain' ),
'labels' => $labels,
'description' => '',
'public' => true,
'hierarchical' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'query_var' => true,
'can_export' => true,
'delete_with_user' => true,
'has_archive' => true,
'rest_base' => '',
'show_in_menu' => true,
'menu_position' => '',
'menu_icon' => 'dashicons-format-audio',
'capability_type' => 'post',
'supports' => ['title', 'editor', 'thumbnail'],
'taxonomies' => ['singer', 'rapper', 'category', 'movie', 'album', 'composer', 'lyricist', 'by-year', 'mood', 'file-link', 'post-id', 'yt_id', 'artist'],
'rewrite' => array('slug' => 'songs'),
];
register_post_type( 'songs', $args );
}
And Code to rewrite slug for post permalink
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'singer' );
if( $terms ){
return str_replace( '/songs/' , '/' . $terms[0]->slug . '/' , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
function custom_rewrite_rules() {
add_rewrite_rule(
'^(.*)/(.*)/?$',
'index.php?post_type=songs&name=$matches[2]',
'top'
);
}
add_action( 'init', 'custom_rewrite_rules' );
thank you ahead for any help.