15

I have a custom post type called slide. It has content and some custom fields. I want WP to show the single.php theme or single-slide.php theme when the user clicks the slide single page link. the problem I have is that WP throws 404 error, when I want to see the single slide page.

here is my code in functions.php:

function create_slide_post_type() {
    $labels = array(
        'name'               => _x( 'اسلایدها', 'i2sa' ),
        'singular_name'      => _x( 'اسلاید', 'i2sa' ),
        'menu_name'          => _x( 'اسلایدها', 'i2sa' ),
        'name_admin_bar'     => _x( 'اسلاید', 'i2sa' ),
        'add_new'            => _x( 'افزودن', 'i2sa' ),
        'add_new_item'       => __( 'افزودن اسلاید جدید', 'i2sa' ),
        'new_item'           => __( 'اسلاید جدید', 'i2sa' ),
        'edit_item'          => __( 'ویرایش اسلاید', 'i2sa' ),
        'view_item'          => __( 'نمایش اسلاید', 'i2sa' ),
        'all_items'          => __( 'همه اسلایدها', 'i2sa' ),
        'search_items'       => __( 'جستجوی اسلایدها', 'i2sa' ),
        'parent_item_colon'  => __( 'اسلایدهای مادر:', 'i2sa' ),
        'not_found'          => __( 'اسلایدی پیدا نشد.', 'i2sa' ),
        'not_found_in_trash' => __( 'اسلایدی در زباله دان پیدا نشد.', 'i2sa' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => "اسلایدهای شرکت i2sa",
        'public'             => true,
        'exclude_from_search'=> true,
        'publicly_queryable' => false,
        'show_in_nav_menus'  => false,
        'menu_icon'          => 'dashicons-images-alt2',
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'slide' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
    );
    register_post_type( 'slide', $args );
    flush_rewrite_rules();
}
add_action( 'init', 'create_slide_post_type' );
38

You should set your publicly_queryable argument to true when registering your custom post type.

TAKE NOTE: Add flush_rewrite_rules(), refresh the page once or twice and REMOVE IT IMMEDIATELY. You SHOULD NOT keep flush_rewrite_rules() unless under the provisions as in the codex.

this is an expensive operation so it should only be used when absolutely necessary

  • Oh wow, that worked here, too. That was completely non-obvious. Thank you! – Toby Deshane Jan 28 at 19:02
  • Worked for me! Thanks! – JamesHoux Jun 10 at 18:58
  • Yes! Finally after doing both it worked. – Eric Jul 24 at 13:42
12

Flush the rewrite rule from dashboard -> Settings->Permalink page. Click on save button and then check your slide details page. It will work now. Otherwise you can write this code flush_rewrite_rules() in your create_slide_post_type() function. See the Codex

  • 2
    Clicking on the save button in Permlinks worked for me. After I moved the site to a production server, all the custom post type links were breaking. This fixed it. Thanks! – cbloss793 Oct 11 '17 at 17:29
  • +1 Just clicking save in Permalinks also worked for me. – MarsAndBack Aug 12 '18 at 0:52
  • Saving the permalinks works. But please do not add flush_rewrite_rules() after register_post_type() calls in production code (i.e. inside create_slide_post_type). Use the plugin-activation hook or an option value to determine that you added a new post type – Philipp Jun 14 at 7:31
6

Refresh your permalinks. Go to Admin->Settings->Permalinks and hit Save. It refreshes your permalinks and should help, especially if you are changing rewrite ( 'rewrite' => array( 'slug' => 'slide' ),)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.