admin管理员组文章数量:1130349
Need some help with this URL rewrite. I have 2 custom posts set up, 'company' and 'job'. I have a single-company page, and a single-job page, which work fine. A company can have many jobs, and I'm managing the relation between jobs and company using post meta.
Single company URL is http://..../company/abc-intl, and single job url is http://..../job/photoshop-designer.
I want a single job URL to include the slug of the company it belongs to/is related with. So for above example, it should be
http://..../company/abc-intl/job/photoshop-designer.
I tried a lot with 'add_rewrite_rule', 'add_rewrite_tag', but didn't seem to work. Please any ideas on how to achieve this URL?
Need some help with this URL rewrite. I have 2 custom posts set up, 'company' and 'job'. I have a single-company page, and a single-job page, which work fine. A company can have many jobs, and I'm managing the relation between jobs and company using post meta.
Single company URL is http://..../company/abc-intl, and single job url is http://..../job/photoshop-designer.
I want a single job URL to include the slug of the company it belongs to/is related with. So for above example, it should be
http://..../company/abc-intl/job/photoshop-designer.
I tried a lot with 'add_rewrite_rule', 'add_rewrite_tag', but didn't seem to work. Please any ideas on how to achieve this URL?
Share Improve this question edited May 15, 2012 at 9:58 Rutwick Gangurde asked Sep 6, 2011 at 5:07 Rutwick GangurdeRutwick Gangurde 8,6245 gold badges43 silver badges55 bronze badges 4- Thanks for viewing! I got it done with a lot of trial and errors! (bump sounds too rude!) – Rutwick Gangurde Commented Sep 6, 2011 at 8:08
- Cool... Got a down vote for this! Dunno why! – Rutwick Gangurde Commented May 15, 2012 at 10:00
- 1 It would be useful if you actually shared your solution in an answer. (I'm not the one who downvoted) – scribu Commented May 15, 2012 at 16:38
- Ohh! Thought so! Sure, lemme compile the solution in a proper format... – Rutwick Gangurde Commented May 16, 2012 at 5:02
1 Answer
Reset to default 1Here's the solution if anyone comes across such a 'weird' requirement ;) Also, when the job CPT (or any CPT you want to associate) is published, you need to save the id of the company CPT (whose slug will be used for forming the other part of the permalink) as a post meta for the job.
<?php
add_filter('init', 'add_page_rewrite_rules');
function add_page_rewrite_rules(){
global $wp_rewrite, $wp;
add_rewrite_rule('^company/([^/]+)/job/([^/]+)', 'index.php?company=$matches[1]&job=$matches[2]', 'top');
$job_structure = '/job/%job%';
$wp_rewrite->add_rewrite_tag("%job%", '([^/]+)', "job=");
$wp_rewrite->add_permastruct('job', $job_structure, false);
}
add_filter('post_type_link', 'job_permalink', 10, 3);
function job_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
'%job%',
'job'
);
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
job_link = '';
if ( strpos($permalink, 'job') !== false ) {
$company_id = get_post_meta($post->ID, 'job_company_id', true);
$company = basename(get_permalink($company_id));
$job_link = 'company'.'/'.$company;
}
$rewritereplace = array(
$post->post_name,
$job_link.'/job'
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
else {
// if they're not using the fancy permalink option
}
return $permalink;
}
?>
Hope this helps someone! You can contact me if stuck while implementing this.
Need some help with this URL rewrite. I have 2 custom posts set up, 'company' and 'job'. I have a single-company page, and a single-job page, which work fine. A company can have many jobs, and I'm managing the relation between jobs and company using post meta.
Single company URL is http://..../company/abc-intl, and single job url is http://..../job/photoshop-designer.
I want a single job URL to include the slug of the company it belongs to/is related with. So for above example, it should be
http://..../company/abc-intl/job/photoshop-designer.
I tried a lot with 'add_rewrite_rule', 'add_rewrite_tag', but didn't seem to work. Please any ideas on how to achieve this URL?
Need some help with this URL rewrite. I have 2 custom posts set up, 'company' and 'job'. I have a single-company page, and a single-job page, which work fine. A company can have many jobs, and I'm managing the relation between jobs and company using post meta.
Single company URL is http://..../company/abc-intl, and single job url is http://..../job/photoshop-designer.
I want a single job URL to include the slug of the company it belongs to/is related with. So for above example, it should be
http://..../company/abc-intl/job/photoshop-designer.
I tried a lot with 'add_rewrite_rule', 'add_rewrite_tag', but didn't seem to work. Please any ideas on how to achieve this URL?
Share Improve this question edited May 15, 2012 at 9:58 Rutwick Gangurde asked Sep 6, 2011 at 5:07 Rutwick GangurdeRutwick Gangurde 8,6245 gold badges43 silver badges55 bronze badges 4- Thanks for viewing! I got it done with a lot of trial and errors! (bump sounds too rude!) – Rutwick Gangurde Commented Sep 6, 2011 at 8:08
- Cool... Got a down vote for this! Dunno why! – Rutwick Gangurde Commented May 15, 2012 at 10:00
- 1 It would be useful if you actually shared your solution in an answer. (I'm not the one who downvoted) – scribu Commented May 15, 2012 at 16:38
- Ohh! Thought so! Sure, lemme compile the solution in a proper format... – Rutwick Gangurde Commented May 16, 2012 at 5:02
1 Answer
Reset to default 1Here's the solution if anyone comes across such a 'weird' requirement ;) Also, when the job CPT (or any CPT you want to associate) is published, you need to save the id of the company CPT (whose slug will be used for forming the other part of the permalink) as a post meta for the job.
<?php
add_filter('init', 'add_page_rewrite_rules');
function add_page_rewrite_rules(){
global $wp_rewrite, $wp;
add_rewrite_rule('^company/([^/]+)/job/([^/]+)', 'index.php?company=$matches[1]&job=$matches[2]', 'top');
$job_structure = '/job/%job%';
$wp_rewrite->add_rewrite_tag("%job%", '([^/]+)', "job=");
$wp_rewrite->add_permastruct('job', $job_structure, false);
}
add_filter('post_type_link', 'job_permalink', 10, 3);
function job_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
'%job%',
'job'
);
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
job_link = '';
if ( strpos($permalink, 'job') !== false ) {
$company_id = get_post_meta($post->ID, 'job_company_id', true);
$company = basename(get_permalink($company_id));
$job_link = 'company'.'/'.$company;
}
$rewritereplace = array(
$post->post_name,
$job_link.'/job'
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
else {
// if they're not using the fancy permalink option
}
return $permalink;
}
?>
Hope this helps someone! You can contact me if stuck while implementing this.
本文标签: url rewritingCombine 2 different custom post slugs into a single permalink
版权声明:本文标题:url rewriting - Combine 2 different custom post slugs into a single permalink? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749141519a2322281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论