WordPress fallback rewrite

Since you want to check if a custom post exists and if not, then load a page, you can’t simply rely on two rewrite rules because the second rule will not act as a fallback. Instead, you can handle this programmatically.

Give this a try:

First, add a custom query var to hold your potential car post/page identifier.

function custom_query_vars_filter($vars) {
  $vars[] = 'car_slug';
  return $vars;
}
add_filter('query_vars', 'custom_query_vars_filter');

Then, you can add a rewrite rule that points to this query var.

function custom_rewrite_rule() {
    add_rewrite_rule(
        '^aaa/bbb/(.*)$',
        'index.php?car_slug=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_rewrite_rule');

Now, the trickier part is to determine whether to load a car post or a page. You can use the template_redirect action to handle this logic.

function custom_template_redirect() {
    $car_slug = get_query_var('car_slug');

    if ($car_slug) {
        // Check if there's a car post with this slug
        $car_post = get_page_by_path($car_slug, OBJECT, 'car');
        if ($car_post) {
            // We found a car post, so we load it
            $GLOBALS['wp_query']->query(array('post_type' => 'car', 'name' => $car_slug));
            include(get_template_directory() . '/single-car.php');
            exit;
        } else {
            // No car post found, check if there's a page
            $page = get_page_by_path($car_slug);
            if ($page) {
                // We found a page, so load it
                $GLOBALS['wp_query']->query(array('page_id' => $page->ID));
                include(get_template_directory() . '/page.php');
                exit;
            }
        }
    }
}
add_action('template_redirect', 'custom_template_redirect');

This way, it first checks if there’s a custom post of type car with the given slug. If we find it, we load it. If not, we then check for a page with that slug and load it if it exists.

I hope this works on your setup.

techhipbettruvabetnorabahisbahis forumuedusedusedueduseduedusedusedusedusedu