3
votes

I've made a WordPress website people can rent cars from brokers. So I have 2 custom post types:

  • broker
  • car

There are about 10 different cars on the website. All these cars are for each broker exactly the same.

So I've created a page template page-brokers.php that lists all the brokers. If you click on a broker, you go to the detail page of the broker single-broker.php

The URL is saferental.be/broker/broker-name

On the detail page of the broker, you'll see all the 10 different cars. If you click on a car, you go to the car detail page, which is single-car.php

The URL is saferental.be/car/car-name

At the bottom of the car detail page, is a form to contact the broker you've selected previously. As you see in the URL, there is nothing mentioned of the selected broker.

When you select a broker, its information is saved in a session and pushed into hidden fields in the form.

Everything works perfectly: - You select a broker - You pick a car -> go to the car detail page - Submit the car detail page form which sends the information to the selected broker.

The result I want to have: - If you select a broker, you go to the detail page saferental.be/broker/broker-name/ (this is already okay) - If you select a car, you go to the car detail page which should be something like this: saferental.be/broker/broker-name/car/car-name

Test website: http://safelease.houston-1.hybridmedia.be/

The brokers are at the bottom of the homepage.

2
Can the same car, be in different brokers? - Stender
you could use ACF and select the cars broker with a relationship field, and use permalink manager (supports ACF) to use that field in the url. - Stender
Yes, the same car can be in different broker. My newly created car "Volvo V40" is available for broker x, y and z. It's the same car. - Dennis
You could create custom rewrite rules to handle the URL structure you want (/broker/broker-name/car/car-name); however, it might be easier for you to use plugin like Redirection - and with this plugin, just create a new redirect with these settings: a) "Source URL": /broker/([^/]+)/car/([^/]+) b) "Regex" checkbox: checked c) "When matched": Pass-through d) "Target URL": /car/$2 - Sally CJ

2 Answers

1
votes

Do you want to keep that car detail page also will available under saferental.be/car/car-name?

If not, so — just put car detail page as child page to broker and url for this page will be saferental.be/broker/broker-name/car/car-name

If you want to many different urls for the same page — try to use this plugin https://wordpress.org/plugins/mapping-multiple-urls-redirect-same-page/

0
votes

I have done something similar to this myself.

This would go in your functions file:

// Setup rewrite rules something like http://yourdomain.com/broker/my-broker/car/my-car
add_action( 'init', 'rewrites_init' );
function rewrites_init() {
    add_rewrite_rule(
        'broker/([-a-zA-Z0-9]+)/car/([-a-zA-Z0-9]+)$',
        'index.php?broker=$matches[1]&car=$matches[2]',
        'top' );
}

// Add variables
add_filter('query_vars', 'add_query_vars', 0);
function add_query_vars($vars) {
    $vars[] = 'broker';
    $vars[] = 'car';
    return $vars;
}

// catch the request for this page
add_action('parse_request', 'parse_requests', 0);
function parse_requests() {
    global $wp, $wp_query;
    if(isset($wp->query_vars['broker']) && isset($wp->query_vars['car'])) {
        // find the car post
        $posts = new WP_Query( array(
            'post_type' => 'car',
            'name' => $wp->query_vars['car'],
            'post_status' => 'publish'
        ));
        if(!empty($posts) ) {
            // set the global query or set your own variable
            $wp_query = $posts;
            // set the broker variable to use in your template
            $broker = get_page_by_path( $wp->query_vars['broker'], OBJECT, 'broker' );
            // include your custom post type template
            if (include(locate_template('single-car.php', true))) {
                exit();
            }
        } else {
            // handle error
            $wp_query->set_404();
            status_header(404);
            locate_template('404.php', true);
            exit;
        }
    }
}

Then in your car template, you should be able to access the $broker post variable.

After you have setup your rewrite rules, you may need to go to Admin -> Settings -> Permalinks and save to set the rewrites.

Hopefully this helps you.