WordPress.org

Ready to get started?Download WordPress

Forums

Permalinks go 404 on custom post types (7 posts)

  1. amg-smit
    Member
    Posted 2 years ago #

    I've read through a lot of support threads but there seems to be no proper solution yet on the following:

    I've followed the codex how to register a custom post type, 'auctions'. Also i've set up a permalink structure: /%postname%/.

    According to the codex and some tutorials, once i've created the files archive-auctions.php and single-auctions.php, it should work like this:

    - http://domain.tld/auctions/ for a loop of all auctions
    - http://domain.tld/auctions/some-post-title/ for viewing a single auction

    All I got was a 404, on both situations. Only resetting the permalink structure to "standard" would do the job. I've tried a lot of things, tried all hints given in all the threads, like:

    - being sure there's no 'double' use of 'auctions', for example in taxonomies
    - re-saving my permalinks settings
    - renewing the .htaccess and more like that
    - and more like that...

    Now, there finally came up a solution, but what I understand from a lot of people this is a very dirty solution: just before the closing tag of register_post_type, putting in the following string: flush_rewrite_rules( false );

    I don't know the consequences of using this method, and there seems no clear information on this string, not even in the codex. That leaves me with two questions:

    1. Can I keep using this method, what are the consequences?
    2. Is there any alternative method?
  2. Andrew Nacin
    Lead Developer
    Posted 2 years ago #

    Can I keep using this method, what are the consequences?

    No. Consequences are decreased performance, potentially drastic, in load times, memory usage, and more.

    Activate your plugin that registers the post types (on the init hook, I hope), then go to Settings > Permalinks and refresh once or twice. Things should work.

    To do this in a plugin you need to distribute, you'll need to call flush_rewrite_rules on an activation hook, like so:

    register_activation_hook( __FILE__, 'flush_rewrite_rules' );

    But you'll also first need to register the post types ON ACTIVATION. Do do that, just register the function that registers your post types BEFORE the line of code above:

    register_activation_hook( __FILE__, 'nacin_register_auctions_post_type' );
    register_activation_hook( __FILE__, 'flush_rewrite_rules' );
  3. amg-smit
    Member
    Posted 2 years ago #

    Ok, so what you're actually saying, is that I should register the post type with a plugin? Because that's not what all the tutorials say, so this is kind of new to me. Untill now, I've put the code for registering the post type in the functions.php.

    I think I know what to do for now: learning to create plugins, never did that before :-)

  4. Andrew Nacin
    Lead Developer
    Posted 2 years ago #

    There's no real requirement to do it in a plugin rather than in a theme. (It's a cognitive/conceptual thing, really -- code is code.) While themes don't have activation hooks, you can still just register the post type on init and then refresh Settings > Permalinks once or twice. Then things should work.

  5. amg-smit
    Member
    Posted 2 years ago #

    But that's exactly my point. I can keep refreshing settings > Permalinks but it doesn't make a difference. So I guess a plugin is the only way to go.

  6. amg-smit
    Member
    Posted 2 years ago #

    Ok, I can't figure out what makes the difference, but registering my post types through a simple plugin, as advised by Andrew (read the first comment on this topic) and only calling the flush-function on activation does the job. The code is simple, feel free to use and adjust:

    <?php
    
    /*
    Plugin Name: Custom post types
    Plugin URI: http://wordpress.org/support/topic/permalinks-go-404-on-custom-post-types?replies=1#post-2301511
    Description: This plugin registers CPT's and runs flush_rewrite_rules() on activation, which in some extraordinary cases makes automatic permalinking work. For more information see the help topic http://wordpress.org/support/topic/permalinks-go-404-on-custom-post-types?replies=1#post-2301511
    Author: Josse Zwols & Geert Smit
    Version: 1.6
    Author URI: http://forgia.nl
    */
    
    add_action( 'init', 'create_my_post_types' );
    
    function create_my_post_types() {
    	register_post_type( 'auctions',
    		array(
    			'labels' => array(
    				'name' => __( 'Auctions' ),
    				'singular_name' => __( 'Auction' )
    			),
    			'public' => true,
    			'has_archive' => true,
    		)
    	);
    	//flush_rewrite_rules( false );
    }
    
    register_activation_hook( __FILE__, 'create_my_post_types' );
    register_activation_hook( __FILE__, 'flush_rewrite_rules' );
    
    ?>
  7. amg-smit
    Member
    Posted 2 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic