Add multiple custom post type in URL
up vote
2
down vote
favorite
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.
wordpress url custom-post-type
|
show 9 more comments
up vote
2
down vote
favorite
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.
wordpress url custom-post-type
Can the same car, be in different brokers?
– Stender
Nov 9 at 14:07
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
Nov 9 at 14:09
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 Perremans
Nov 9 at 14:22
1
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
Nov 13 at 18:06
1
Let us continue this discussion in chat.
– Sally CJ
Nov 15 at 10:32
|
show 9 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
wordpress url custom-post-type
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.
wordpress url custom-post-type
wordpress url custom-post-type
edited Nov 9 at 13:11
Nasser Ali Karimi
901128
901128
asked Nov 9 at 12:52
Dennis Perremans
11911
11911
Can the same car, be in different brokers?
– Stender
Nov 9 at 14:07
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
Nov 9 at 14:09
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 Perremans
Nov 9 at 14:22
1
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
Nov 13 at 18:06
1
Let us continue this discussion in chat.
– Sally CJ
Nov 15 at 10:32
|
show 9 more comments
Can the same car, be in different brokers?
– Stender
Nov 9 at 14:07
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
Nov 9 at 14:09
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 Perremans
Nov 9 at 14:22
1
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
Nov 13 at 18:06
1
Let us continue this discussion in chat.
– Sally CJ
Nov 15 at 10:32
Can the same car, be in different brokers?
– Stender
Nov 9 at 14:07
Can the same car, be in different brokers?
– Stender
Nov 9 at 14:07
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
Nov 9 at 14:09
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
Nov 9 at 14:09
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 Perremans
Nov 9 at 14:22
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 Perremans
Nov 9 at 14:22
1
1
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
Nov 13 at 18:06
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
Nov 13 at 18:06
1
1
Let us continue this discussion in chat.
– Sally CJ
Nov 15 at 10:32
Let us continue this discussion in chat.
– Sally CJ
Nov 15 at 10:32
|
show 9 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
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/
Yes, the cars may only be available under /broker/broker-name/car/car-name But how do you make a detail page a child? I know that you can make a "normal" page a child. But my child page is single-car.php ...
– Dennis Perremans
Nov 10 at 7:59
I've tried the plugin you've mentioned. But what RegularExpression do I have to use? I've tried: ^broker/car/ and then Select Type = Custom Post type (Car). But /broker/broker-name/car/car-name isn't available yet.
– Dennis Perremans
Nov 10 at 8:35
I've contacted the developer of that plugin you've mentioned, but it's not possible. That plugin simply redirects pages.
– Dennis Perremans
Nov 13 at 14:29
In WordPress you can set parent to any page. Just in details page set parent page as specific broker.
– amedv
Nov 13 at 16:35
add a comment |
up vote
0
down vote
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226054%2fadd-multiple-custom-post-type-in-url%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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/
Yes, the cars may only be available under /broker/broker-name/car/car-name But how do you make a detail page a child? I know that you can make a "normal" page a child. But my child page is single-car.php ...
– Dennis Perremans
Nov 10 at 7:59
I've tried the plugin you've mentioned. But what RegularExpression do I have to use? I've tried: ^broker/car/ and then Select Type = Custom Post type (Car). But /broker/broker-name/car/car-name isn't available yet.
– Dennis Perremans
Nov 10 at 8:35
I've contacted the developer of that plugin you've mentioned, but it's not possible. That plugin simply redirects pages.
– Dennis Perremans
Nov 13 at 14:29
In WordPress you can set parent to any page. Just in details page set parent page as specific broker.
– amedv
Nov 13 at 16:35
add a comment |
up vote
0
down vote
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/
Yes, the cars may only be available under /broker/broker-name/car/car-name But how do you make a detail page a child? I know that you can make a "normal" page a child. But my child page is single-car.php ...
– Dennis Perremans
Nov 10 at 7:59
I've tried the plugin you've mentioned. But what RegularExpression do I have to use? I've tried: ^broker/car/ and then Select Type = Custom Post type (Car). But /broker/broker-name/car/car-name isn't available yet.
– Dennis Perremans
Nov 10 at 8:35
I've contacted the developer of that plugin you've mentioned, but it's not possible. That plugin simply redirects pages.
– Dennis Perremans
Nov 13 at 14:29
In WordPress you can set parent to any page. Just in details page set parent page as specific broker.
– amedv
Nov 13 at 16:35
add a comment |
up vote
0
down vote
up vote
0
down vote
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/
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/
answered Nov 9 at 21:56
amedv
1379
1379
Yes, the cars may only be available under /broker/broker-name/car/car-name But how do you make a detail page a child? I know that you can make a "normal" page a child. But my child page is single-car.php ...
– Dennis Perremans
Nov 10 at 7:59
I've tried the plugin you've mentioned. But what RegularExpression do I have to use? I've tried: ^broker/car/ and then Select Type = Custom Post type (Car). But /broker/broker-name/car/car-name isn't available yet.
– Dennis Perremans
Nov 10 at 8:35
I've contacted the developer of that plugin you've mentioned, but it's not possible. That plugin simply redirects pages.
– Dennis Perremans
Nov 13 at 14:29
In WordPress you can set parent to any page. Just in details page set parent page as specific broker.
– amedv
Nov 13 at 16:35
add a comment |
Yes, the cars may only be available under /broker/broker-name/car/car-name But how do you make a detail page a child? I know that you can make a "normal" page a child. But my child page is single-car.php ...
– Dennis Perremans
Nov 10 at 7:59
I've tried the plugin you've mentioned. But what RegularExpression do I have to use? I've tried: ^broker/car/ and then Select Type = Custom Post type (Car). But /broker/broker-name/car/car-name isn't available yet.
– Dennis Perremans
Nov 10 at 8:35
I've contacted the developer of that plugin you've mentioned, but it's not possible. That plugin simply redirects pages.
– Dennis Perremans
Nov 13 at 14:29
In WordPress you can set parent to any page. Just in details page set parent page as specific broker.
– amedv
Nov 13 at 16:35
Yes, the cars may only be available under /broker/broker-name/car/car-name But how do you make a detail page a child? I know that you can make a "normal" page a child. But my child page is single-car.php ...
– Dennis Perremans
Nov 10 at 7:59
Yes, the cars may only be available under /broker/broker-name/car/car-name But how do you make a detail page a child? I know that you can make a "normal" page a child. But my child page is single-car.php ...
– Dennis Perremans
Nov 10 at 7:59
I've tried the plugin you've mentioned. But what RegularExpression do I have to use? I've tried: ^broker/car/ and then Select Type = Custom Post type (Car). But /broker/broker-name/car/car-name isn't available yet.
– Dennis Perremans
Nov 10 at 8:35
I've tried the plugin you've mentioned. But what RegularExpression do I have to use? I've tried: ^broker/car/ and then Select Type = Custom Post type (Car). But /broker/broker-name/car/car-name isn't available yet.
– Dennis Perremans
Nov 10 at 8:35
I've contacted the developer of that plugin you've mentioned, but it's not possible. That plugin simply redirects pages.
– Dennis Perremans
Nov 13 at 14:29
I've contacted the developer of that plugin you've mentioned, but it's not possible. That plugin simply redirects pages.
– Dennis Perremans
Nov 13 at 14:29
In WordPress you can set parent to any page. Just in details page set parent page as specific broker.
– amedv
Nov 13 at 16:35
In WordPress you can set parent to any page. Just in details page set parent page as specific broker.
– amedv
Nov 13 at 16:35
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 20 at 23:40
CUGreen
1,8501511
1,8501511
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226054%2fadd-multiple-custom-post-type-in-url%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can the same car, be in different brokers?
– Stender
Nov 9 at 14:07
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
Nov 9 at 14:09
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 Perremans
Nov 9 at 14:22
1
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
Nov 13 at 18:06
1
Let us continue this discussion in chat.
– Sally CJ
Nov 15 at 10:32