I'm building a custom theme using Xampp on my localhost. I've got everything sorted for the site, except for this permalinks issue.

Currently, the site uses the Advanced Custom Fields plugin and has some dependencies on this plugin to work.

When using plain permalinks, the site works fine. When I change to "Post name" permalinks, all pages on the site return 404 errors, except for the Custom Post Types that I've setup, which function as normal pages.

I can revert back to one of the default themes, and the permalinks start working as normal, so I'm fairly confident that it's a theme issue.

When I replace the functions.php file with a blank file (no php code inside), the site loads minus the styles as expected, but all the links simply refresh the page instead of attempting to navigate to any of the pages.

Just to confirm a few things...

  • mod_rewrite is turned on in Apache, although I've also set this up on a separate Microsoft server and IIS, so I don't think this is the issue.
  • I've disabled all plugins (including the advanced custom posts), and the 404 issue still occurs
  • The pages don't return 404's when switched to the default theme
  • The home page renders normally regardless of what settings I use.
  • I'm getting the 404.php page (that I setup) returned, and not the WordPress 404 page
  • I've tried adding rules to flush the rewrite rules
  • I've tried setting global permalink rewrite structure to /%postname% which didn't make a difference
  • WordPress can write to the .htaccess file. This definitely changes or is created locally when permalink settings are updated so this isn't an issue either.

Below is my functions.php code which initializes the Custom Post to WordPress - this is pretty much the only thing that I can think of that must contain some sort of error, as all of the above steps that I've tried don't seem to have any effect.

/* Add custom post type 'Products' to Tower/NPI Theme */
  add_action('init', 'create_postTypeProducts', 0 );
  function create_postTypeProducts() {

$labels = array(
  'name'                => _x( 'Products', 'Post Type General Name', 'Tower-NPI'),
  'singular name'       => _x( 'Product', 'Singular Name', 'Tower-NPI'),
  'menu_name'           => _x( 'Products', 'admin menu', 'Tower-NPI'),
  'name_admin_bar'      => _x( 'Products', 'add new on admin bar', 'Tower-NPI' ),
  'all_items'           => __( 'All Products', 'Tower-NPI' ),
  'view_item'           => __( 'View Products', 'Tower-NPI' ),
  'add_new_item'        => _x( 'Add New Product', 'Tower-NPI' ),
  'add_new'             => _x( 'Add Product', 'Tower-NPI' ),
  'edit_item'           => __( 'Edit Product', 'Tower-NPI' ),
  'update_item'         => __( 'Update Product', 'Tower-NPI' ),
  'search_items'        => __( 'Search for Products', 'Tower-NPI' ),
  'not_found'           => __( 'Not Found', 'Tower-NPI' ),
  'not_found_in_trash'  => __( 'Not found in Trash', 'Tower-NPI' ),
);

$args = array(
  'label'               => __('Products', 'Tower-NPI'),
  'description'         => __('A list of products associated with the Tower/NPI Theme.', 'Tower-NPI'),
  'labels'              => $labels,
  'supports'            => array( 'title', 'editor', 'excerpt', 'revisions', 'author', 'thumbnail', 'custom-fields', 'post-formats', 'page-attributes' ),
  'hierarchical'        => true,
  'public'              => true,
  'show_ui'             => true,
  'show_in_menu'        => true,
  'show_in_nav_menus'   => true,
  'show_in_admin_bar'   => true,
  'menu_position'       => 21,
  'menu_icon'           => 'dashicons-admin-home',
  'can_export'          => true,
  'has_archive'         => true,
  'exclude_from_search' => false,
  'publicly_queryable'  => true,
  'capability_type'     => 'post',
  'show_in_rest'        => true,
  'rest_base'           => 'products-api',
  'rest_controller_class' => 'WP_REST_Posts_Controller',
  'rewrite'             => array('slug' => '/', 'with_front' => false,)
);
register_post_type('Products', $args );
  }

 /* Flush rewrite rules on theme switch */
  function my_rewrite_flush() {
    create_postTypeProducts();
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );

Any help would be hugely appreciated.

Thanks,

up vote 0 down vote accepted

The slug you use for the CPT looks very weird. You should inspect the rewrite rules being used in parsing a request with something like the query monitor plugin, but the gut feeling is that your CPT slug makes all URLs to match it and since there is no CPT for the URL of a post, you get a 404.

OTOH if you are really getting the server's 404 it means you have a configuration problem and you should check your htaccess rules, make sure they are default.

Side note: always prefix, prefix, prefix everything which might end being in the global scope like the CPT type name.

  • Thank you sir! That was the issue. TBH though, I had previously commented out that line and refreshed the page and was still getting the same issue. This time, I removed it, flushed the rewrite rules, and now working perfectly. Sending virtual high fives your way! – Cactusman07 Feb 27 at 20:29

Is the my_cpy_init() function defined when the theme is switched? It might be causing an issue if it's not.

You can try adding this to your functions.php to see if just flushing the rewrite rules helps, outside of the theme switch hook.

add_action( 'init', 'flush_on_init' );
function flush_on_init(){
    flush_rewrite_rules();
}
  • Sorry - it was, but I've hit undo, commented out & changed the function name before copy / pasting here lol. I've updated the above code to include the type. Thanks, I'll give this a go. – Cactusman07 Feb 27 at 20:15
  • No luck unfortunately. That didn't work. – Cactusman07 Feb 27 at 20:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.