WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Permalinks 404 with custom post type. (40 posts)

  1. onelli
    Member
    Posted 3 years ago #

    I'm currently having a permalink problem that I can't seem to track down.
    I'm using WP 3.0 with some custom post_types that I've made.
    In this example, I'm using the custom post type "event".

    This is my custom permalink structure:
    /%category%/%postname%/

    This an example of the permalink that it provides me, that 404's:
    http://mysite.com/event/heres-a-title/

    However, if I enter the "default permalink structure" in to the url, such as:
    http://mysite.com/?event=heres-a-title

    Then it redirects me to this, which works:
    http://mysite.com/event/heres-a-title/?event=heres-a-title

    It's weird how "/event/heres-a-title/" doesn't work (the permalink that wordpress gives me), but "/event/heres-a-title/?event=heres-a-title" does.

    I followed some standard tutorials on how to properly create custom post types, even a tutorial that was supposed to stop these 404's.

    Here's my register_post_type:

    register_post_type('event', array(
    			'label' => __('Events'),
    			'singular_label' => __('Event'),
    			'public' => true,
    			'show_ui' => true,
    			'_builtin' => false,
    			'_edit_link' => 'post.php?post=%d',
    			'capability_type' => 'post',
    			'hierarchical' => false,
    			'rewrite' => array("slug" => "event"),
    			'query_var' => "event",
    			'supports' => array('title','editor')
    ));

    Any help would be greatly appreciated. Even just a workaround to have all my custom post types permalink to the working structure would be great.

    Thanks.

  2. MichaelH
    Member
    Posted 3 years ago #

    Q: How can custom post types be set to use my permalink settings?
    ;In short, they can't. Custom post types don't use the permalink setting structure. See Otto's explanation: http://lists.automattic.com/pipermail/wp-testers/2010-May/013010.html

  3. onelli
    Member
    Posted 3 years ago #

    I guess I was misled in to using custom post types to begin with, but I'm still okay with this because I don't need it to do the things listed in Otto's explanation:

    - show up in "the blog" - No
    - show up in the normal feeds - No
    - fit with the normal permalink structure - No

    I just want the links to work, in any structure. When I query the posts, I want to be able to link to them. Either using the_permalink() or something else. Any ideas on how I might do that? Is there a way to reference the slug-ready title of the post?

    Being able to do base_url + "/?event=" + post_title_slug would be fine with me.

    I don't need it to use my custom permalink structure, I just want to be able to point to the post without a 404.

    Thanks.

  4. onelli
    Member
    Posted 3 years ago #

    I'm using a lame workaround, but it works for now.

    I removed the %category% from my permalink structure. Having that somehow makes the custom posts 404. Now I'm just using a date structure.

    Thanks for your help.

  5. Decio
    Member
    Posted 3 years ago #

    I found this but haven´t tested yet: http://somadesign.ca/projects/smarter-custom-post-types/

  6. Leroy12
    Member
    Posted 3 years ago #

    I got the same issue, on the 4th custom types I have added to my site... No way to figure what was wrong. So just to make things clear, the solution has explained in the link Decio posted is:

    "One note that’s very, very important: your custom URLs won’t work until you go to Options → Permalink in wp-admin and re-save your current URL structure. This will flush WP’s current URL structure and add our new rewrite rules."

    Thanks,
    Nicolas

  7. robbono
    Member
    Posted 3 years ago #

    I am having the

      exact
    same problem as the original poster described.

    I have my overall permalink structure set to:
    /%category%/%postname%.

    If I browse to
    http://mysite.com/?event=heres-a-title

    I get redirected successfully to
    http://mysite.com/event/heres-a-title/?event=heres-a-title

    But if I browse to the link that WordPress generates:
    http://mysite.com/event/heres-a-title/

    I end up with a 404.

    I've tried to rebuild my .htaccess file using Settings > Permalinks, but this has not fixed the issue.

    I don't expect the custom content types to work with with my permalink structure in terms of categories; I only expect that the Rewrite function for WordPress's register_post_type() function will work as described. Unfortunately, it does not seem to work.

    Changing my permalink structure doesn't fix the problem.

    Is there anyone out there who has actually successfully implemented custom post types with url rewriting?

    For your reference, the code I'm using is:

    'add_action( 'init', 'create_post_type' );
    function create_post_type() {
    register_post_type( 'event',
    array(
    'labels' => array(
    'name' => __( 'Events' ),
    'singular_name' => __( 'Event' )
    ),
    'public' => true,
    'rewrite' => true,
    'show_ui' => true, // UI in admin panel
    '_builtin' => false, // It's a custom post type, not built in!
    '_edit_link' => 'post.php?post=%d',
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array('title','author')
    )
    );
    }

    Is this a bug? Misconfiguration on my end? A misunderstanding of what "rewrite" means in the Codex? Any help would be much appreciated.

  8. robbono
    Member
    Posted 3 years ago #

    Note: I've also read through Otto's responses on the automattic mailing list, as seen here:

    http://lists.automattic.com/pipermail/wp-testers/2010-May/013017.html

    If I read this correctly, he seems to be saying that Permalinks for custom content types won't work at all if you have %postname% in your Settings > Permalinks permalink structure. Can someone confirm for me that this is true?

    If so...well, that's a little klorbed, I suppose, since almost everyone I know uses %postname% in their permalinks for SEO purposes.

  9. John Fulwider
    Member
    Posted 3 years ago #

    I am also having the same problem. I encountered it today when I changed a site I had been operating CMS-style, with a static page displayed on the front page ...

    http://fulwiderpartners.com

    ... to include blog posts on the front page.

    Any help would be appreciated.

  10. banesto
    Member
    Posted 3 years ago #

    any updates on this subject?
    it seems that function 'register_post_type' does not generate new rewrites it's supposed to.

  11. flashpunk
    Member
    Posted 3 years ago #

    Hey there,
    I'm having the same issue here, has anyone found a resolution to this?

  12. nalins
    Member
    Posted 3 years ago #

    Any ideas?

  13. anmari
    Member
    Posted 3 years ago #

    Hi all,
    permalinks and custom post types work fine for me - one must have a prefix for the custom post type and MUST not use that prefix for anything else that may appear in a permalink structure.

    EG: if you have a category called event and you use category in your permalink structure, that will cause a clash with an 'event' custom post type permalink.

    My permalink settings:

    /%post_id%/%postname%/

    It ensures a unique url with no possibility of a clash (post id makes std posts unique)

    The UNIQUEness of the permalink must apply to taxonomies too I imagine

  14. banesto
    Member
    Posted 3 years ago #

    I've managed to accomplish exactly what i needed with custom post type rewrite rules. Here's a brief explanation and code

  15. davidfcarr
    Member
    Posted 3 years ago #

    I'm looking for help on a related problem here:

    http://wordpress.org/support/topic/need-help-diagnosing-permalink-problem-reports?replies=1

    I saw the post about not letting the same term occur elsewhere in your permalink structure. But when I tried changing my 'event' post type to 'rsvp-event' it made no difference on the system of a user who was having trouble with 404 errors. Yet it works fine on my site and those of some other happy users of the plugin.

  16. postoy
    Member
    Posted 3 years ago #

    This might work for some of you:

    After adding a custom post type, you may need to navigate to the Permalinks Settings page. There's no need to update or Save Changes.

  17. amuralikumar
    Member
    Posted 3 years ago #

    Here is the solution
    in the register_post_type arguments, there is an rewrite argument.
    you should make it like this :
    'rewrite' => array( 'slug' => 'product','with_front' => FALSE),
    view the example below :

    add_action('init', 'usb_init');
    function usb_init()
    {
        $labels = array(
        'name' => _x('USB', 'post type general name'),
        'singular_name' => _x('USB', 'post type singular name'),
        'add_new' => _x('Add New', 'usb'),
        'add_new_item' => __('Add New USB'),
        'edit_item' => __('Edit USB'),
        'new_item' => __('New USB'),
        'view_item' => __('View USB'),
        'search_items' => __('Search USBs'),
        'not_found' =>  __('No usb found'),
        'not_found_in_trash' => __('No usb found in Trash'),
        'parent_item_colon' => ''
    );
      $args = array(
        'labels' => $labels,
        'public' => false,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'product','with_front' => FALSE),
    	'capability_type' => 'post',
    	'hierarchical' => false,
        'menu_position' => '5',
        'supports' => array('title','excerpt','editor','thumbnail','page-attributes'),
    	'taxonomies' => false
    );
    register_post_type('usb',$args);
    }
  18. amuralikumar
    Member
    Posted 3 years ago #

    Ask me if you have any question :)

  19. davidfcarr
    Member
    Posted 3 years ago #

    I've seen other advice that the with_front rewrite argument should be set to true or it won't work with some setups. Could you please explain your theory and/or experience with that?

  20. amuralikumar
    Member
    Posted 3 years ago #

  21. amuralikumar
    Member
    Posted 3 years ago #

    you can find more details here: http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/

    And further , you need to make sure, if you have a custom post type "product" , Then you should not have a page named "products".

    If you have a page named by the plural form of the custom post type, then you can rename its slug, and add change the rewrite as above and then, go to permalink page, just change and restore the setting. and check if the custom post is working. if working ,
    then finally, you can again rename the page slug to "products"

  22. Jorge A. Gonzalez
    Member
    Posted 3 years ago #

    @amuralikumar

    Thanks, that works for me.

    Here is my code

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'store',
        array(
          'labels' => array(
            'name' => __( 'Stores' ),
            'singular_name' => __( 'Store' )
          ),
          'menu_position' => 2,
          'supports' => array('title','editor','author','thumbnail','trackbacks','custom-fields','comments','revisions','page-attributes'),
          'public' => true,
          'hierarchical' => false,
          '_builtin' => false,
          'capability_type' => 'post',
          'rewrite' => array('slug' => 'store','with_front' => FALSE)
    
        )
      );
    }
  23. mcnamee
    Member
    Posted 3 years ago #

    I still can't get this to work...

    The ONLY way it works is by adding flush_rewrite_rules() in the function which creates the post type. But from what I understand, this isn't good to do.

    I've tried a number of things:

    • rewriting the slug
    • changing the permalinks under settings
    • making sure page names and custom posts aren't the same
    • clearing the rewrite rules from the database
    • Flush rewrite - which only works when it's permanently there

    Is there anything else I can try?

    Link: http://alive.barasites.com/

    // CUSTOM POST TYPE
    add_action( 'init', 'create_post_type' );
    
    function create_post_type() {
      register_post_type( 'Events',
        array(
          'labels' => array(
            'name' => __( 'Events' ),
            'singular_name' => __( 'Events' )
          ),
          'public' => true,
    	  'supports' => array('title','editor','author','thumbnail','excerpt','comments')
        )
      );
    
      register_post_type( 'Sermons',
        array(
          'labels' => array(
            'name' => __( 'Sermons' ),
            'singular_name' => __( 'Sermons' )
          ),
          'public' => true,
    	  'supports' => array('title','editor','author','thumbnail','excerpt','comments')
        )
      );
    
      register_post_type( 'Photo Gallery',
        array(
          'labels' => array(
            'name' => __( 'Photo Gallery' ),
            'singular_name' => __( 'Photo Gallery' )
          ),
          'public' => true,
    	  'supports' => array('title', 'thumbnail')
        )
      );
      flush_rewrite_rules();
    }
  24. mcnamee
    Member
    Posted 3 years ago #

    Sorry for posting again so soon, but another thing that appears to work is setting

    'rewrite' => false

    Not sure if this helps come to a solution?

  25. mattifesto
    Member
    Posted 3 years ago #

    I have tried about everything and read a bunch of the related code in WordPress. The only thing that worked for me is to add:

    flush_rewrite_rules();

    after my call(s) to register_post_type. But at least that totally works.

  26. astrotim
    Member
    Posted 3 years ago #

    I confirm that mattifesto's solution also worked for me. I have been using permalinks with my custom post types without this additional function until recently; I'm not sure why it is now necessary - perhaps a core change in a recent WordPress update.

    I have added this function to the code in my post about custom post types here:
    astronautdesigns.com/2010/06/wordpress-3-custom-post-types/#permalink_404

  27. mxmai
    Member
    Posted 3 years ago #

    I've found you only need to call flush_rewire_rules once after you've set up your custom post type, not every time you load the page.

    still a bit mysterious how it's all working though.

  28. Devin Walker
    Member
    Posted 3 years ago #

    How about if you're using a plugin to create custom post types like Custom Post Type UI: http://wordpress.org/extend/plugins/custom-post-type-ui/

    Any ideas?

    Thanks guys.

  29. Devin Walker
    Member
    Posted 3 years ago #

    Ok I just resolved my issue, here's the deal:

    For my scenario I was using Custom Post Types to display Press articles. I created a page named "Press" with and a custom post type named "press" as well. WordPress does not like this at all.

    You CANNOT have a page with the same name as a custom post type.

    I needed to replace the custom post type's name in the MySQL database so here's simple query that will find-and-replace an old custom post type name with a new one:

    UPDATE wp_posts SET post_type = REPLACE(post_type, 'old-post-type-name', 'new-post-type-name');

    I hope this solves some issues for you folks. I'm compiling a list of Custom post type 404 error scenarios and solutions here: http://www.wordimpressed.com/wordpress/solve-wordpress-custom-post-type-404-error-issues/ Please contact me if you have any input!

  30. DDT
    Member
    Posted 3 years ago #

    I haven't tested amuralikumar solution with the with_front setting, but for now the solution %post_id% seem to be working.

    My permalink structure looks like:
    /%category%/%post_id%/%postname%

Topic Closed

This topic has been closed to new replies.

About this Topic