0

I'd like use a specific custom field in a custom post type as the slug for the post.

My custom post type is members and the custom field that I want to use as the slug is member_user. How can I do this?

2
  • 2
    Why not use the "Edit" button underneath the title field which already does this?
    – Howdy_McGee
    Oct 9, 2015 at 15:21
  • I'm actually not using Title at all in the custom post, which is part of the problem. I created the custom field member_user to associate a member with a User. The full name of the User ought to be the title, but I haven't set it, figuring I can just do it on the front end. So now the Titles, and in turn the slugs get set to auto-draft automatically. But ideally I'd like to have the User's name be the Title and the slug. Oct 9, 2015 at 15:39

1 Answer 1

1

You could do this like saving post meta. You would unhook out of the current hook and update the post, if you don't do that you will end up in an infinite loop:

/**
 * Save Custom Data
 * @param int $post_id
 * @param Post Object $post
 */
function save_custom_meta_boxes( $post_id, $post ) {

    // If we're not in the right place, bailout
    if( ! isset( $post ) || wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
        return $post_id;
    }

    // `members` Type Metaboxes
    if( 'members' == $post->post_type ) {

        // Update Post Slug
        if( isset( $_POST['member_user'] ) && ! empty( $_POST['member_user'] ) ) {
            remove_action( 'save_post', 'save_custom_meta_boxes' );
            wp_update_post( array( 'ID' => $post_id, 'post_name' => sanitize_title( $_POST['member_user'] ) ) );
            add_action( 'save_post', 'save_custom_meta_boxes' );
        }
    }
}
add_action( 'save_post', 'save_custom_meta_boxes', 10, 2 );

IF the user has set a value into the member_user field we will remove the current action, update the post to the new slug using the same field, and rehook the action to continue processing any other post meta values. We take advantage of sanitize_title() to ensure the user doesn't add anything malicious.

4
  • Pasting this in to functions.php doesn't seem to work. Breaks the page. And don't I need to add filter so this function will get called in the first place? Oct 9, 2015 at 16:30
  • @user1902789 not having the action shouldn't break the page so something else is happening. You may need to turn on Debugging to figure out what exactly. I've edited the above code to add the hook needed...
    – Howdy_McGee
    Oct 9, 2015 at 16:51
  • Ah - you missed a closed parenthesis in the wp_update_post function. Page isn't breaking now but the slug is pulling from the title, not the custom field. Oct 9, 2015 at 17:11
  • @user1902789 Ensure that the custom field is called member_user and not something different. Ensure that members is actually the post type name. Additionally you may need to print_r($_POST); die(); to get to the bottom of the issue. I just tested the above on a custom install and it worked as expected.
    – Howdy_McGee
    Oct 9, 2015 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.