I have some custom permalinks for my custom post types, that look like so:
/products/category/subcategory/product-name
I wrote a generate_rewrite_rules
function for the permalinks related to that custom post type.
However, any regular pages I create via WP's interface show up as 404s, while those custom permalinks still work. I'm convinced it's an Apache problem. Can't figure out what's wrong.
I have an editable .htaccess in place, with the normal stuff inside...
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
My apache conf file for the relevant server:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /path/to/folder>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
Order allow,deny
allow from all
</Directory>
EDIT Running WP 4.8.1, on Ubuntu 14.04
EDIT 2: Just noticed this is a WP 404, not an apache 404. So this is down to my rewrite rules, and I've noticed that the rewrite is looking for every page being one of my custom taxonomy, as in (index.php?product-category=$matches[1]) and that's why I get the 404. Of course this shouldn't happen.
How do I fix it? My function is a bit of a nest.
function custom_rewrite_rules($wp_rewrite) {
$newRules = array();
$terms = get_terms( array(
'taxonomy' => 'product-category',
'hide_empty' => false,
) );
foreach ($terms as $term) {
$post_query = new WP_Query( array(
'post_type' => 'custom_product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product-category',
'field' => 'slug',
'terms' => $term->slug,
'include_children' => false
),
)
));
if ( $post_query->have_posts() ) : while ( $post_query->have_posts() ) : $post_query->the_post();
if($term->parent){
$parent = get_term( $term->parent, 'product-category');
$newRules['products/' . $parent->name . '/' . $term->name . '/'. get_post_field( 'post_name', get_the_ID() ) . '$'] = 'index.php?taxonomy=product-category&post_type=custom_product&product-category='.$parent->slug.'&product-category='.$term->slug.'&custom_product='. get_post_field( 'post_name', get_the_ID() );
} else {
$newRules['products/' . $term->name . '/'. get_post_field( 'post_name', get_the_ID() ) . '$'] = 'index.php?taxonomy=product-category&post_type=custom_product&product-category='.$term->slug.'&custom_product='.get_post_field( 'post_name', get_the_ID() );
}
endwhile;
endif;
if($term->parent){
$parent = get_term( $term->parent, 'product-category');
$newRules['products/' . $parent->name . '/' . $term->name . '$'] = 'index.php?taxonomy=product-category&product-category='.$parent->slug.'&product-category='.$term->slug;
} else {
$newRules['products/' . $term->name . '$'] = 'index.php?taxonomy=product-category&product-category='.$term->slug;
}
}
$newRules['products/(.+)/(.+)/?$'] = 'index.php?post_type=custom_product&custom_product=$matches[2]';
$newRules['products$'] = 'index.php?post-type=page&pagename=product-categories';
$wp_rewrite->rules = $newRules + $wp_rewrite->rules;
}