The problem is with that add_rewrite_tag('%keyterms%', '(.*)')
, which results in rewrite conflicts because the generated rewrite rules would match http://example.com/(anything-here)
and that (anything-here)
can be a Page (post type page
) slug, hence http://example.com/page-slug
would not work — you wouldn’t get a 404
error page, but the proper Page wouldn’t be displayed, either.
So looking at your code, instead of using that (.*)
(which matches anything), you should instead specify the exact value, which in your case is either apartments-for-rent
or homes-for-sale
:
add_rewrite_tag('%keyterms%', '(apartments-for-rent|homes-for-sale)');
That should work, but be sure to flush the rewrite rules — just visit the permalink settings page.
UPDATE
Sorry, I wasn’t aware WordPress isn’t retaining the RegEx for all rewrite rules, specifically for attachments where WordPress removes the brackets ((
and )
):
// Relevant code in WP_Rewrite::generate_rewrite_rules().
$submatchbase = str_replace( array( '(', ')' ), '', $match );
And I know you changed the RegEx to ([aehomnprt]+s-for-[aelnrst]+)
which does work, although not as precise as the (apartments-for-rent|homes-for-sale)
.
So apart from the alternate RegEx, you can make (apartments-for-rent|homes-for-sale)
works like so (this code would go in the theme functions file):
add_filter( 'rewrite_rules_array', 'fix_inventory_rewrite_rules' );
function fix_inventory_rewrite_rules( $rules ) {
$rules2 = [];
$values="apartments-for-rent|homes-for-sale";
$_re = $values . "https://wordpress.stackexchange.com/";
$my_re = preg_quote( $_re, "https://wordpress.stackexchange.com/" );
foreach ( $rules as $regex => $query ) {
if ( preg_match( '/^' . $my_re . "https://wordpress.stackexchange.com/", $regex ) ) {
$regex = str_replace( $_re, '(' . $values . ')/', $regex );
}
$rules2[ $regex ] = $query;
}
return $rules2;
}