skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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;
    }
    }
    

  2. I have tested your code and everything works fine, however, I want to add some information that might help your understanding of the problem.

    When your post is published the preview url will be like this:

    https://dev.test/cars/test/?preview_id=113&preview_nonce=6cf651d710&preview=true
    

    When you’re post is not published the preview url will be like this, because it’s not published it will not give you a permalink,

    https://dev.test/?post_type=news&p=115&preview=true
    

    so if you’ll try https://dev.test/cars/115/?preview=true for drafted posts, it will give you 404 for sure.

    In your examples, I am guessing 22 is your post ID and when you use https://example.com/cars/22/?preview=true without publishing the post you get 404, which is right.

    and when you publish the post and use https://example.com/cars/22/test?preview=true and it will redirect you to https://example.com/cars/test that is also right.

    So everything is right with your code nothing is wrong.

    CONCLUSION

    If you need to preview an unpublished post you’ll have to use https://example.com?post_type=news&p=22&preview=true, this url pattern.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search