WordPress自定义URL 404求助:修改自定义文章固定链接后页面无法访问
Hey there! I totally get how frustrating it is to inherit someone else’s plugin code and hit a 404 when you think you’ve nailed the permalink setup. Let’s walk through the key fixes you can implement directly in your plugin code to get that http://xxxx.com/jobs/2015-1114/ link working properly.
1. Double-Check Your Custom Post Type (CPT) Registration
First, locate where the jobs CPT is registered in the plugin code. The rewrite parameters here make or break your permalink mapping—if they’re off, WordPress won’t recognize your URL structure.
Look for code like register_post_type( 'jobs', ... ) and ensure the rewrite array includes these critical settings:
'rewrite' => array( 'slug' => 'jobs', // Matches the "jobs" segment in your desired URL 'with_front' => false, // Prevents default prefixes (like /blog/) from being added to your URL 'pages' => true // Ensures pagination works if you ever need it ),
Also confirm these essential parameters are set to true (they’re required for public permalinks to function):
'public' => true, 'publicly_queryable' => true, 'has_archive' => true, // Optional but useful if you need a jobs archive page later
2. Force a Rewrite Rule Flush (Don’t Skip This!)
WordPress doesn’t automatically update its rewrite rules when you tweak CPT settings. Since you can’t use other plugins or the admin dashboard, add this code to your plugin to flush rules when the plugin activates:
// Flush rewrite rules on plugin activation function my_jobs_flush_rewrite_rules() { // Re-register the CPT first to ensure rules are generated correctly register_post_type( 'jobs' ); flush_rewrite_rules(); } register_activation_hook( __FILE__, 'my_jobs_flush_rewrite_rules' );
To trigger this:
- Deactivate then reactivate your plugin. This runs the flush once (you can leave this code in for future activations, it’s safe).
3. Customize the Permalink Structure (If Needed)
If 2015-1114 is a custom slug (not the default post name), explicitly define the permalink structure for the CPT with this function:
function my_jobs_custom_permastruct() { // Map "jobs/{slug}" to the jobs CPT add_permastruct( 'jobs', 'jobs/%postname%', array( 'with_front' => false, 'ep_mask' => EP_PERMALINK | EP_PAGES // Enables proper permalink and page handling ) ); } add_action( 'init', 'my_jobs_custom_permastruct', 10 );
If your URL uses a date+ID pattern instead of a slug, you’d need to adjust the structure and add rewrite tags, but based on your example, this should cover it.
4. Debug Rewrite Rules (If 404s Still Happen)
If you’re still seeing errors, check if WordPress is generating the right rewrite rules. Add this temporary debug code to your plugin:
// Temporary: Print all rewrite rules to debug function my_jobs_debug_rewrite_rules() { global $wp_rewrite; print_r( $wp_rewrite->rules ); exit; } add_action( 'init', 'my_jobs_debug_rewrite_rules', 20 );
Visit your site’s homepage, and you’ll see a list of active rules. Look for one matching jobs/([^/]+)/?$—it should point to index.php?post_type=jobs&name=$matches[1] (or post_id if using IDs instead of slugs). If this rule is missing, go back to step 1 and recheck your CPT parameters.
Once confirmed, remove the debug code.
Final Quick Checks
- Make sure the post with slug
2015-1114actually exists under thejobsCPT. - Clear any site caching (if used) to ensure new rules load properly.
内容的提问来源于stack exchange,提问作者Chris Hunter




