admin管理员组文章数量:1122846
I'm using wp-types to build a wordpress site. I'm happy with the types and their fields, I just wanted to created a function that shows parents in the url - see blow function that achieves this - also using the Custom Post Type Permalinks plugin to add my custom tag %parents% to the url.
The rewrite seems to work fine, displays in the back end etc. But when you click view document - 404.php.
I've tried rewrite flushing - mod rewrite definitely works, as friendly permalinks work for pages and don't 404, it's only on the custom post types.
My ideal url structure is: %sector%/%parents%/%postname% - Sector being a custom taxonomy. The custom taxonomy also displays fine because of the CPTP plugin but 404s as well.
wp-types does have rewrite functions but they are all turned off. it's really got me stumped, the code below is included in my functions.php - I am using bones starter theme.
Should I include this code as a plugin instead? Or is there something i've missed? The only way I can get access the items is by setting permalinks to default.
add_filter('post_type_link', 'parents_post_rewrite_function', 10, 3);
function parents_post_rewrite_function( $post_link, $id = 0, $leavename = FALSE ) {
$post = get_post($id);
$postID = $post->ID;
// Always globalize $wpdb
global $wpdb;
// Get current posts parent type
$parentType = $wpdb->get_var("SELECT `meta_key` FROM `wp_postmeta` WHERE `post_id` ={$postID} AND `meta_key` LIKE '%belongs%'");
// Get current posts parentID if project
$parentID = $wpdb->get_var("SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` ={$postID} AND `meta_key` = '{$parentType}'" );
if(!empty($parentID)){
// Get parent parent posts type
$parentParentType = $wpdb->get_var("SELECT `meta_key` FROM `wp_postmeta` WHERE `post_id` ={$parentID} AND `meta_key` LIKE '%belongs%'");
// Try and get same value from parent ID, if fails then no parent of parent
$parentParentID = $wpdb->get_var("SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` ={$parentID} AND `meta_key` = '{$parentParentType}'" );
}
if(!empty($parentParentID)){
$parentslug = get_post($parentID)->post_name;
$parentparentslug = get_post($parentParentID)->post_name;
$parents = $parentparentslug.'/'.$parentslug;
} else {
$parents = get_post($parentID)->post_name;
}
// Get parents parent post ID & continue until none found
// Convert IDs to names and render slug
return str_replace('%parents%', $parents, $post_link);
}
I'm using wp-types to build a wordpress site. I'm happy with the types and their fields, I just wanted to created a function that shows parents in the url - see blow function that achieves this - also using the Custom Post Type Permalinks plugin to add my custom tag %parents% to the url.
The rewrite seems to work fine, displays in the back end etc. But when you click view document - 404.php.
I've tried rewrite flushing - mod rewrite definitely works, as friendly permalinks work for pages and don't 404, it's only on the custom post types.
My ideal url structure is: %sector%/%parents%/%postname% - Sector being a custom taxonomy. The custom taxonomy also displays fine because of the CPTP plugin but 404s as well.
wp-types does have rewrite functions but they are all turned off. it's really got me stumped, the code below is included in my functions.php - I am using bones starter theme.
Should I include this code as a plugin instead? Or is there something i've missed? The only way I can get access the items is by setting permalinks to default.
add_filter('post_type_link', 'parents_post_rewrite_function', 10, 3);
function parents_post_rewrite_function( $post_link, $id = 0, $leavename = FALSE ) {
$post = get_post($id);
$postID = $post->ID;
// Always globalize $wpdb
global $wpdb;
// Get current posts parent type
$parentType = $wpdb->get_var("SELECT `meta_key` FROM `wp_postmeta` WHERE `post_id` ={$postID} AND `meta_key` LIKE '%belongs%'");
// Get current posts parentID if project
$parentID = $wpdb->get_var("SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` ={$postID} AND `meta_key` = '{$parentType}'" );
if(!empty($parentID)){
// Get parent parent posts type
$parentParentType = $wpdb->get_var("SELECT `meta_key` FROM `wp_postmeta` WHERE `post_id` ={$parentID} AND `meta_key` LIKE '%belongs%'");
// Try and get same value from parent ID, if fails then no parent of parent
$parentParentID = $wpdb->get_var("SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` ={$parentID} AND `meta_key` = '{$parentParentType}'" );
}
if(!empty($parentParentID)){
$parentslug = get_post($parentID)->post_name;
$parentparentslug = get_post($parentParentID)->post_name;
$parents = $parentparentslug.'/'.$parentslug;
} else {
$parents = get_post($parentID)->post_name;
}
// Get parents parent post ID & continue until none found
// Convert IDs to names and render slug
return str_replace('%parents%', $parents, $post_link);
}
Share
Improve this question
edited Feb 6, 2015 at 6:01
Nathan Wright
asked Feb 6, 2015 at 5:55
Nathan WrightNathan Wright
92 bronze badges
2
|
1 Answer
Reset to default 0I had this same problem when I cloned my site to set up a development server - for me, it working by setting permalinks to ugly, saving, then setting them to pretty again, which rebuilt whatever had gone wrong in htaccess.
本文标签: Custom post type permalink tag 404
版权声明:本文标题:Custom post type permalink tag 404 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310037a1934234.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
post_type_link
filter is just for functions that output permalinks, it has nothing to do with generating rewrite rules for incoming requests for those permalinks. – Milo Commented Feb 6, 2015 at 6:55