I want to have permalinks which will be look like: example.com/mypost/%post-id%-%slug%/
And the following code do it. But when I try to go to url of the post... I get error 404.
I have this code:
To register my custom post type function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'My Post', 'Post Type General Name', 'mypage' ),
'singular_name' => _x( 'My Post', 'Post Type Singular Name', 'mypage' ),
'menu_name' => __( 'My Post', 'mypage' ),
'parent_item_colon' => __( 'Rodzic ogłoszenia', 'mypage' ),
'all_items' => __( 'Wszystkie ogłoszenia', 'mypage' ),
'view_item' => __( 'Zobacz My Post', 'mypage' ),
'add_new_item' => __( 'Dodaj My Post', 'mypage' ),
'add_new' => __( 'Dodaj nowe', 'mypage' ),
'edit_item' => __( 'Edytuj', 'mypage' ),
'update_item' => __( 'Zapisz zmiany', 'mypage' ),
'search_items' => __( 'Szukaj ogłoszenia', 'mypage' ),
'not_found' => __( 'Nie znaleziono', 'mypage' ),
'not_found_in_trash' => __( 'Nie znaleziono w koszu', 'mypage' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'myposts', 'mypage' ),
'description' => __( 'Ogłoszenia Nieruchomości w Holandii.', 'mypage' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
// 'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'menu_icon' => 'dashicons-category',
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 25,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rewrite' => array( 'slug' => _x('mypost/%post_id%','Slug myposts','mypage'))
);
}
And for change the permalink (slug):
function custom_post_mycustomname_link($post_link, $post = 0, $leavename = false) {
if($post->post_type == 'myposts') {
return str_replace('%post_id%/', $post->ID.'-', $post_link);
}
else {
return $post_link;
}
}
add_filter('post_type_link', 'custom_post_mycustomname_link', 1, 3);
But the problem is that when I try to go to post I get error 404. I try to re-save premalinks in settings and it does not help. Can somone help?