Custom post type single page returns 404 error
-
17-04-2021 - |
Question
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' );
Solution
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
OTHER TIPS
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
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' ),)
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.