WordPress.org

Ready to get started?Download WordPress

Forums

Custom rewrite rules go missing when using rewrite=> array(slug=>'foo') (8 posts)

  1. magicroundabout
    Member
    Posted 1 year ago #

    Hi folks,

    I've got a weird problem on a number of sites that I'm trying to diagnose.

    These sites are using custom themes with built-in custom post types. The post types are registered using an action on the init hook. e.g.

    add_action( 'init', 'register_cpt_my_type' );
    function register_cpt_my_type() {
       $args = ...;
       register_post_type( 'my_type', $args );

    Some of my themes have custom post types set up using:

    has_archive => true,
    rewrite => array( 'slug' => 'my_types' ),

    Others have:

    has_archive => 'my_types',
    rewrite=>true

    The problem I have ONLY affect those sites/themes using this always thought that these would be equivalent. I've learned that this is not the case, but won't go into detail here.

    Every now and then sites using the former approach (rewrite=> array('slug'=>'string') suddenly have the post type archive stop working - I get a "Not found" message (i.e. an 404 page).

    This can be fixed by saving the permalink options to refresh the rewrite rules, but it then happens again a few weeks later (the exact schedule is unclear).

    This does not happen on sites where I don't set the rewrite option, but use has_archive => 'string' instead.

    Interestingly I've been going round installing the Rewrite Rules Inspector plugin, and on the one site that is now failed, I get "missing" rewrite rules for that CPT.

    Does anyone have any idea what's happening to my rewrite rules? Why would they go missing every few weeks? Why does it not happen with has_archive=>'string'? Any ideas?

    Thanks

    Ross

  2. Marble23
    Member
    Posted 1 year ago #

    Hi Ross,

    Are you experiencing this issues as well? I am running into the same problem. My custom post types work fine only if I set the permalink settings to "default". All other options breaks them. I get the 404 page. I've set these up numerous times in the past. But on a brand new site with 3.5.1 its not working.

    The plugin: "Rewrite Rules Inspector" shows them all missing. I've flushed the rules numerous times to no avail.

    *edit* Also noticing Category and Tag rewrite rules are not getting generated either. Author, Search, comments etc are getting written.

  3. Marble23
    Member
    Posted 1 year ago #

    Ok, not sure if this is a bug or not. What I noticed is the "query_var" of the post type needs to match the "slug" in the rewrite rule.

    What I was doing (not company name is hidden for client)

    'query_var' => 'thecompanyname_store'

    then in the rewrite rule:
    'slug' => 'store'

    This doesn't seem to work. I think this was breaking when generating the rewrite rules, which caused the categories and tags to not generate either. Flushing rules, changing so both query var and slug matched fixed the problem.

  4. magicroundabout
    Member
    Posted 1 year ago #

    Hey Marble23

    Are you the guy I was chatting with in IRC yesterday? I actually use a combination of:

    'has_archive' => 'new_slug'

    and

    'rewrite' => array( 'slug' => 'new_slug' )

    That seems to work, but I've never had an explanation of why I need both!

    Ross

  5. Marble23
    Member
    Posted 1 year ago #

    Was not me on IRC. But thats good to know about the has_archive. What I don't understand is why you would need to configure both. Seems archive should inherit from the rewrite by default? Maybe I am missing something.

  6. Randhir Singh Paul
    Member
    Posted 8 months ago #

    Hi,
    am using custom post type and i want to remove the post slug name from url am did a lots of tricks but nothing worked.Can anyone tell me the solution for it.

  7. Randhir Singh Paul
    Member
    Posted 8 months ago #

    function aitDirItemPostType() {

    register_post_type( 'ait-dir-item',

    array(

    'labels' => array(

    'name' => 'Items',

    'singular_name' => 'Item',

    'add_new' => 'Add new',

    'add_new_item' => 'Add new item',

    'edit_item' => 'Edit item',

    'new_item' => 'New item',

    'not_found' => 'No items found',

    'not_found_in_trash' => 'No items found in Trash',

    'menu_name' => 'Items',

    ),

    'description' => 'Manipulating with items',

    'public' => true,

    'show_in_nav_menus' => true,

    'supports' => array(

    'title',

    'thumbnail',

    'author',

    'editor',

    'excerpt',

    'comments',

    ),

    'show_ui' => true,

    'show_in_menu' => true,

    'menu_icon' => AIT_FRAMEWORK_URL . '/CustomTypes/dir-item/dir-item.png',

    'menu_position' => $GLOBALS['aitThemeCustomTypes']['dir-item'],

    'has_archive' => true,

    'query_var' => true,

    'rewrite' =>true,

    'rewrite' => array('slug' => 'items', 'with_front' => false, 'hierarchical' => false),

    'capability_type' => 'ait-dir-item',

    'map_meta_cap' => true

    )

    );

  8. Randhir Singh Paul
    Member
    Posted 8 months ago #

    i used 'with_front' => false bt not working.??

Topic Closed

This topic has been closed to new replies.

About this Topic