I want to change the slug of a post with a custom field.
In example, if the custom field is “keyword” my post link will become: mysite.com/keyword.
If the custom field is empty, i want to generate a random key like mysite.com/xV5f7A.
How can i do that without change the wordpress core?
Any ideas are welcome! Thanks.
The slug is saved on the wp_posts while custom fields are on the wp_posts_meta. If you want to make it like that you can use an action hook on save_post that will get the value of the custom fields and saves it as the post slug.
Here is the code
add_action('save_post', 'set_slug');
function set_slug($post_id){
$new_slug = get_post_meta($post_id,'custom-slug', true);
$post_args = array(
'ID' => $post_id,
'post_name' => $new_slug,
);
wp_update_post($post_args);
}