I have created a CPT and created add rewrite rule using the following code to achieve the following URL structure:
https://www.example.com/coupons/company/
function custom_rewrite_basic()
{
add_rewrite_rule('^coupons/([a-z0-9-]+)[/]?$', 'coupons/?host=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
add_action( 'template_include', function( $template ) {
if ( get_query_var( 'host' ) == false || get_query_var( 'host' ) == '' ) {
return $template;
}
return get_theme_file_path().'/list-coupons.php';
} );
I have placed the above code in functions.php of child theme but I get a 404 error. The same code worked when successfully when I had uploaded this code in functions.php file of parent theme.
Note: list-coupons.php file exists as a custom template in child theme.
Can anyone help me solve this issue?