0

I am not able to rewrite custom post type permalink.

 $labels = array(
            'name' => __('VmGallery', THEME_TEXT_DOMAIN, THEME_TEXT_DOMAIN),
            'singular_name' => __('VmGallery', THEME_TEXT_DOMAIN),
            'add_new' => __('Add New', THEME_TEXT_DOMAIN),
            'add_new_item' => __('Add New Gallery', THEME_TEXT_DOMAIN),
            'edit_item' => __('Edit Gallery', THEME_TEXT_DOMAIN),
            'new_item' => __('New Gallery', THEME_TEXT_DOMAIN),
            'all_items' => __('All Galleries', THEME_TEXT_DOMAIN),
            'view_item' => __('View Gallery', THEME_TEXT_DOMAIN),
            'search_items' => __('Search Gallery', THEME_TEXT_DOMAIN),
            'not_found' => __('No galleries found', THEME_TEXT_DOMAIN),
            'not_found_in_trash' => __('No galleries found in the Trash', THEME_TEXT_DOMAIN),
            'parent_item_colon' => '',
            'menu_name' => __('Gallery', THEME_TEXT_DOMAIN),
        );
$supports = array('title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'post-formats', 'author', 'excerpt');

 $args = array(
            'labels' => $labels,
            'description' => 'Galleries specific information',
            'public' => true,
            'menu_position' => 6,
            'menu_icon' => 'dashicons-format-image',
            'supports' => $supports,
            'taxonomies' => array('gallery_categories', 'post_tag'),
            'has_archive' => 'ggg'
            /*'rewrite' => array(
                'slug' => $slug,
                'with_front' => false
            ),*/
        );

        register_post_type('vmgallery', $args);

I have php file: archive-vmgallery.php I think that it should be loaded when URL is myurl.com/ggg. But it shows 404 error page instead. If I use post type instead of 'ggg': myurl.com/vmgallery, then archive-vmgallery.php is loaded. Why? I thought that 'has_archive' => 'ggg' allows to rewrite slug?

1
  • 1
    Did you try re-saving the permalink in settings ?
    – Sumit
    Commented Mar 1, 2016 at 13:16

1 Answer 1

2

Every time you change rewrite rules, you need to rebuild the rewrite rules in the database. You can do that flush_rewrite_rules() function, but don't use it in every page load, you only need to do it once. In general, a very good hook to do it is the register_actionvation_hook() of your plugin. You should rebuild the rewrite rules also when your plugin is deactivated in order to remove you custom rewrite rules:

register_activation_hook( __FILE__, function () {
    cyb_register_vmgallery_post_type();
    flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, function () {
    flush_rewrite_rules();
} );
add_action( 'init', 'cyb_register_vmgallery_post_type' );
function cyb_register_vmgallery_post_type() {
   $labels = array(
            'name' => __('VmGallery', THEME_TEXT_DOMAIN),
            'singular_name' => __('VmGallery', THEME_TEXT_DOMAIN),
            'add_new' => __('Add New', THEME_TEXT_DOMAIN),
            'add_new_item' => __('Add New Gallery', THEME_TEXT_DOMAIN),
            'edit_item' => __('Edit Gallery', THEME_TEXT_DOMAIN),
            'new_item' => __('New Gallery', THEME_TEXT_DOMAIN),
            'all_items' => __('All Galleries', THEME_TEXT_DOMAIN),
            'view_item' => __('View Gallery', THEME_TEXT_DOMAIN),
            'search_items' => __('Search Gallery', THEME_TEXT_DOMAIN),
            'not_found' => __('No galleries found', THEME_TEXT_DOMAIN),
            'not_found_in_trash' => __('No galleries found in the Trash', THEME_TEXT_DOMAIN),
            'parent_item_colon' => '',
            'menu_name' => __('Gallery', THEME_TEXT_DOMAIN),
        );
    $supports = array('title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'post-formats', 'author', 'excerpt');

    $args = array(
            'labels' => $labels,
            'description' => 'Galleries specific information',
            'public' => true,
            'menu_position' => 6,
            'menu_icon' => 'dashicons-format-image',
            'supports' => $supports,
            'taxonomies' => array('gallery_categories', 'post_tag'),
            'has_archive' => 'ggg'
            /*'rewrite' => array(
                'slug' => $slug,
                'with_front' => false
            ),*/
        );

        register_post_type('vmgallery', $args);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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