0

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.

1 Answer 1

0
// change in post type argument
register_post_type(
             "rewrite" => [ "slug" => "songs/%singer%",
 //change in taxonomy argument                         "with_front" => true ]);
 register_taxonomy(
                    'rewrite' =>false',
                    'with_front' => true,
                    'query_var' => true,
                    'Custom Rewrite Slug' => '');
// for add rewrite rule
function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new['songs/([^/]+)/(.+)/?$'] = 'index.php?songs=$matches[2]';
  $new['songs/(.+)/?$'] = 'index.php?singer=$matches[1]';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

// for permalinks
function wpa_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'songs' ){
        $terms = wp_get_object_terms( $post->ID, 'singer' );
        if( $terms ){
            return str_replace( '%singer%' , $terms[0]->slug , $post_link );
        }
        else{
           return str_replace( '%singer%/' , '' , $post_link ); 
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 3 );

Hope this is helps you :)

5
  • Thanks for the reply, Sir I've tried but the result I got changed singer permalink also and the song URL is not as I required. As the solution, a single song link is like example.com/song/singer/song-name/ the required URL pattern is exapmle.com/singer/song-name/ Suggest If anything else I could do, that could be better, thanks in advance.
    – Rashid
    Dec 24, 2021 at 22:38
  • is page show error 404 ? Dec 25, 2021 at 10:11
  • The pattern you provide is giving me URL as example.com/song/singer/song-name/ but I don't need that's pattern bro I want to remove the post type slug and add custom taxonomy before the post URL. Also tried patterns and working fine for single song URL are example .com/songs/singer/song-name and example .com/song-name but when I try to get URL as example .com/singer/song-name all my pagination got broken or got 404 on single song URL
    – Rashid
    Dec 25, 2021 at 13:23
  • oky i try to find and try to make pattern which you want if i got i post here : ) Dec 26, 2021 at 5:51
  • Thanks, @MR. shaikh__92
    – Rashid
    Dec 26, 2021 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.