0

I created a custom post type. It's book and my url look like example.com/book/a-book I show books on example.com/books but there is example.com/book how can I delete that URL or show 404. In this case my sitemap shows that url: example.com/book

function book_post_type() {
    $labels = array(
        'name'                => _x( 'Books', 'Post Type General Name', 'sv' ),
        'singular_name'       => _x( 'Book', 'Post Type Singular Name', 'sv' ),
        'menu_name'           => __( 'Books', 'sv' ),
        'parent_item_colon'   => __( 'Parent Book', 'sv' ),
        'all_items'           => __( 'All Books', 'sv' ),
        'view_item'           => __( 'View Book', 'sv' ),
        'add_new_item'        => __( 'Add New Book', 'sv' ),
        'add_new'             => __( 'Add New', 'sv' ),
        'edit_item'           => __( 'Edit Book', 'sv' ),
        'update_item'         => __( 'Update Book', 'sv' ),
        'search_items'        => __( 'Search Books', 'sv' ),
        'not_found'           => __( 'Not Found', 'sv' ),
        'not_found_in_trash'  => __( 'Not Found In Trash', 'sv' ),
    );
    $args = array(
        'label'               => __( 'books', 'sv' ),
        'description'         => __( 'Books', 'sv' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'thumbnail', 'editor', 'revisions', ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => false,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'book_post_type', 0 );
3
  • nothing in your code here refers to books at all...
    – rudtek
    May 19, 2017 at 3:28
  • I edited it again. There is original code. When I try to connect example.com/book/ it redirects to another post. So It means there no page. But I am using Yoast SEO plugin. When I go to sitemap.xml which has posts of book. I see that URL in sitemap. May 19, 2017 at 3:39
  • your code looks good. can you try refreshing the yoast plugin?
    – rudtek
    May 19, 2017 at 5:12

1 Answer 1

0
function rt_custom_post() {
  register_post_type( 'book',
    array(
    'labels' => array(
        'name' => __( 'Books' ),
        'singular_name' => __( 'Book' ), 
      ),
    'public' => true,
    'has_archive' => false, //this will eliminate the archive page
    'supports' => array('title','editor','thumbnail'),
    'publicly_queryable'  => true
    )
  );

if you add :

'has_archive' => false,

to your register post type, it will remove the archive page. Remember to flush the permalinks by visiting the settings -> permalinks page after you make the change.

Make sure you didn't create a page called book as well.

3
  • It works for 5 custom post types but there is one more I see it in sitemap.xml there is no page called like it and I flush the permalinks. May 19, 2017 at 1:50
  • Can you post your code?
    – rudtek
    May 19, 2017 at 2:01
  • I edited my question May 19, 2017 at 2:26

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.