27

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' );
0

4 Answers 4

59

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

5
  • 1
    Oh wow, that worked here, too. That was completely non-obvious. Thank you! Jan 28, 2019 at 19:02
  • Worked for me! Thanks!
    – JamesHoux
    Jun 10, 2019 at 18:58
  • Yes! Finally after doing both it worked.
    – Eric
    Jul 24, 2019 at 13:42
  • 2
    flush_rewrite_rules() was it for me - nice one! Nov 2, 2020 at 15:13
  • 2
    You can simply try to go to Settings > Permalinks and click save without changing anything. If you don't want to edit your code and are looking for a 'hopefully' quick fix
    – BonisTech
    Dec 2, 2020 at 10:11
24

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

3
  • 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, 2017 at 17:29
  • 1
    +1 Just clicking save in Permalinks also worked for me.
    – Kalnode
    Aug 12, 2018 at 0:52
  • 2
    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, 2019 at 7:31
11

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' ),)

1
  • did it for me - thank you
    – cljk
    Apr 27, 2020 at 9:48
4

You should set the following in your virtualhost apache:

    <Directory /var/www/html/wordpress>
            AllowOverride All
    </Directory>

Then, refresh your permalinks: wp-admin->settings->permalinks. That's worked for me.

1
  • Thank you for posting this answer. Now I at least know that the problem is in my apache configuration and not the code.
    – Sharpey
    Nov 13, 2021 at 22:33

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.