WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Permalinks 404 with custom post type.

[Resolved] Permalinks 404 with custom post type.

  • 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.

Viewing 15 replies - 16 through 30 (of 39 total)
  • 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);
    }

    Ask me if you have any question 🙂

    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?

    you can find more details here:

    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

    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

    @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)
    
        )
      );
    }

    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();
    }

    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?

    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.

    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

    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.

    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.

    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!

    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%

    I have tested mattifesto’s solution and it worked for me.
    Great thank to mattifesto.

Viewing 15 replies - 16 through 30 (of 39 total)
  • The topic ‘[Resolved] Permalinks 404 with custom post type.’ is closed to new replies.