Support » Fixing WordPress » Permalinks go 404 on custom post types

  • 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?
Viewing 6 replies - 1 through 6 (of 6 total)
  • 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' );

    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 🙂

    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.

    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.

    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:

    Plugin Name: Custom post types
    Plugin URI:
    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
    Author: Josse Zwols & Geert Smit
    Version: 1.6
    Author URI:
    add_action( 'init', 'create_my_post_types' );
    function create_my_post_types() {
    	register_post_type( 'auctions',
    			'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' );
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Permalinks go 404 on custom post types’ is closed to new replies.