admin管理员组文章数量:1122846
I've amended the URLs for my custom post type from:
url/jobs/job-name
to url/jobs/job-location/job-name
Problem is, because this isn't a standard rewrite wordpress doesn't do the 301 redirecting for you.
Does anyone know how I could do a 301 redirect for all custom posts to the new URL bearing in mind that job-location would be different depending on the post?
Thanks!
I've amended the URLs for my custom post type from:
url.com/jobs/job-name
to url.com/jobs/job-location/job-name
Problem is, because this isn't a standard rewrite wordpress doesn't do the 301 redirecting for you.
Does anyone know how I could do a 301 redirect for all custom posts to the new URL bearing in mind that job-location would be different depending on the post?
Thanks!
Share Improve this question asked Nov 20, 2018 at 11:23 MichaelMichael 311 bronze badge1 Answer
Reset to default 0Since job-location
is a variable part - which content we do not know beforehand - it is not possible to easily solve this in a .htaccess redirect. You could however generate a list of posts with their old and new URI and place those redirects in the .htaccess - I would prefer it that way because then PHP is not needed at all.
If that is not an option, the following should work
add_action('wp_loaded', 'WPSE_redirect_jobs_new_url');
function WPSE_redirect_jobs_new_url(){
// current request
$uri = $_SERVER['REQUEST_URI'];
$parts = explode('/', $uri);
if (substr($uri, 0, 6) !== '/jobs/' || count($parts) > 3) {
// either URI does not start with jobs
// or URI is already longer than /jobs/foo
// so end early
return;
}
$slug = array_pop($parts);
$page = get_page_by_path($slug, OBJECT, 'MY_CPT');
if (!$page instanceof WP_Post) {
return;
}
// get new link
$link = get_permalink($page->ID);
wp_redirect($link, 301);
exit;
});
Because WordPress has its own way of guessing redirects, it is important to use an early action. In my test, wp_loaded
sufficed for this. Sadly, the global $wp
is not fully populated by that time, so here I am checking the requested URI via $_SERVER['REQUEST_URI']
instead of $wp->request
.
This of course relies on permalinks being set up for jobs as you wish them, otherwise get_permalink()
will not yield the results you're looking for.
本文标签: Bulk 301 redirect for custom post type
版权声明:本文标题:Bulk 301 redirect for custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736291162a1928666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论