I need to be able to rewrite the permalink of custom post type posts to:
{post-id}.domain.com
and if {custom-field-1} of the post has a value, to:
{custom-field-1}.domain.com
Is it possible to do it with the wp_rewrite api, or in the post registering code some how?
BTW - the (awful) way i do it now is by:
Adding a wild card to my domain (*.domain.com).
On my index file i check with php what is the url and if it is a ubdomain or not.
if it is i get the subdomain part ("that-part".domain.com) and check if theres a post with this value in the specific custom field in my posts or is there a post with this id and if there is then is set the $post data to that post and include the right template file for that post type.
I know it's a bad way to that. but that's the only way i know how for now, hope you will help getting to a better solution.
What have you already tried? As wp_rewrite seems like the easiest solution so far.
Not sure how you'd do this for a sub-domain without the use of any .htaccess amends. However, if based on the root domain; the below [untested] solution may work:
function custom_rewrite() {
if ( ! get_post_meta( 'your_key', get_the_ID() ) ) {
add_rewrite_rule( '^' . get_the_ID(), 'index.php?post_id=' . get_the_ID() . ', 'top' );
} else {
add_rewrite_rule( '^' . get_post_meta( 'your_key', get_the_ID() ), 'index.php?post_id=' . get_the_ID() . ', 'top' );
}
}
add_action( 'init', 'custom_rewrite' );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.