The custom post type
function prowpsite_create_custom_post_types()
{
$types = array(
// Where the magic happens
array(
'the_type' => 'news',
'single' => 'car',
'plural' => 'cars',
'rewrite' => 'cars',
'icon' => 'dashicons-admin-site-alt',
),
);
foreach ($types as $type) {
$the_type = $type['the_type'];
$single = $type['single'];
$plural = $type['plural'];
$rewrite = $type['rewrite'];
$icon = $type['icon'];
$labels = array(
'name' => _x($plural, 'post type general name'),
'singular_name' => _x($single, 'post type singular name'),
'add_new' => _x('add' . $type['single'], $single),
'add_new_item' => __('Add New ' . $single),
'edit_item' => __('Edit ' . $single),
'new_item' => __('New ' . $single),
'view_item' => __('View ' . $single),
'search_items' => __('Search ' . $plural),
'not_found' => __('No ' . $plural . ' found'),
'not_found_in_trash' => __('No ' . $plural . ' found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'can_export' => true,
'has_archive' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => true, // To use Gutenberg editor.
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'block-editor' => true,
'rewrite' => array('slug' => $rewrite),
'supports' => array('title', 'editor', 'author', 'thumbnail', 'custom-fields', 'excerpt', 'revisions'),
'menu_icon' => $icon,
);
register_post_type($the_type, $args);
}
}
add_action('init', 'prowpsite_create_custom_post_types');
/* Flush permalinks */
function prowpsite_theme_rewrite_flush()
{flush_rewrite_rules();
}
add_action('init', 'prowpsite_theme_rewrite_flush');`
Why I can't preview the custom post type "car", the preview link return 404!
https://example.com/cars/22/?preview=true
It works when only it published and the link has the slug like this!!
https://example.com/cars/22/test?preview=true
How can I fix it?
Tried to use
add_filter('preview_post_link', 'bitflower_change_post_link', 10, 2);
and also tried
add_filter('preview_post_car_link', 'bitflower_change_post_link', 10, 2);
Saving the permalinks not help
But no way!
Can you help?
It has been working since adding this code.
add_filter('post_type_link', 'prowpsite_change_post_type_link', 1, 3);
function prowpsite_change_post_type_link($link, $post = 0)
{
if ($post->post_type == 'cars' && (strpos($link, "preview") !== false)) {
return home_url('cars/' . $post->ID . '/' . $post->post_name);
} else {
return $link;
}
}