WordPress custom post type link are changed but returns 404

Question

WordPress custom post type link are changed but returns 404.

I have created a custom post type like this,

function create_notes_custom() {

    register_post_type( 'coaches_resources',
        array(
            'labels' => array(
                'name' => __( 'Coaches Resources' ),
                'singular_name' => __( 'Coaches Resource' ),
                'add_new' => __( 'Add New' ),
                'add_new_item' => __( 'Add New Resource' ),
                'edit' => __( 'Edit' ),
                'edit_item' => __( 'Edit Resource' ),
                'new_item' => __( 'New Resource' ),
                'view' => __( 'View' ),
                'view_item' => __( 'View Resource' ),
                'search_items' => __( 'Search Resources' ),
                'not_found' => __( 'No resources found' ),
                'not_found_in_trash' => __( 'No resource found in Trash' ),
                'parent' => __( 'Parent Resource' )
            ),
            'public' => true,
            'menu_position' => 5,
            'supports' => array( 'title' ),
            'taxonomies' => array( 'resource_chapter' ),
            'rewrite' => array( 'slug' => 'resource', 'with_front' => true ),
            'has_archive' => false,
            'hierarchical' => false
        )
    );
}
add_action('init', 'create_notes_custom');

URL is changed by,

function change_link( $permalink, $post ) {
    
    if( $post->post_type == 'coaches_resources' ) {
        $resource_terms = get_the_terms( $post, 'resource_chapter' );
        $term_slug = '';
        if( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $term ) {
            
            
                if (! empty($term->slug))  {
                    
                    // The featured resource will have another category which is the main one
                    if( $term->slug == 'featured' ) {
                        continue;
                    }
    
                    $term_slug = $term->slug;
                    break;
                }
            }
        }
        $permalink = get_home_url() ."/notebook/" . $term_slug . '/' . $post->post_name;
    }
    return $permalink;

}
add_filter('post_type_link', "change_link", 10, 2);

URL is changed from
http://example.com/resource/my-first-note/
to
http://example.com/notebook/note-category/my-first-note

But it returns 404.

0
Alex Brend 2 months 0 Answers 7 views 0

Leave an answer