0

first of all, im sorry for my poor english, when i upadte a post this code changes the post slug to a "profileid" costom field value..

add_action('save_post', 'my_custom_slug');
function my_custom_slug($post_id) {


     //Check it's not an auto save routine
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
 //Perform permission checks! For example:
    if ( !current_user_can('edit_post', $post_id) ) 
        return; 
    //If calling wp_update_post, unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'my_custom_slug');
    //call wp_update_post update, which calls save_post again. E.g:
        wp_update_post(array('ID' => $post_id, 'post_name' =>get_post_meta($post_id,'profileid',true)));
    // re-hook this function
    add_action('save_post', 'my_custom_slug');
}

it works fine, but how i can use this for only a specific custom post type? my custom post type is "masters".. i used this, but not works! anyone can help?

add_action('save_post', 'my_custom_slug');
function my_custom_slug($post_id) {


    $slug = 'masters';
    // If this isn't a 'masters' post, don't update it.
    if ( $slug != $post->post_type )
        return $post_id;


     //Check it's not an auto save routine
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
 //Perform permission checks! For example:
    if ( !current_user_can('edit_post', $post_id) ) 
        return; 
    //If calling wp_update_post, unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'my_custom_slug');
    //call wp_update_post update, which calls save_post again. E.g:
        wp_update_post(array('ID' => $post_id, 'post_name' =>get_post_meta($post_id,'profileid',true)));
    // re-hook this function
    add_action('save_post', 'my_custom_slug');
}
1
  • How you call this function in submit form? I'm trying something like that but when I submit the form, nothing happens. Oct 22 '15 at 13:31
0

You can stop executing the function if the post is not of a specific post type. You can use get_post_type() for this.

if( 'masters' != get_post_type( $post_id ) )
  return;
1
0

Just don't use the action save_post, use "save_post_{$post->post_type}" instead. It fires right before save_post, and you get the same information.

Example for the post type masters:

add_action( 'save_post_masters', 'my_custom_slug', 10, 2 );

function my_custom_slug( $post_id, \WP_Post $post ) {
}
0

/* check for your post type only */

    function your_function_name( $post_id ) {
        global $post;  

        //skip auto save

        //......

        if( $post->post_type == "your_post_type" ) {
                if( isset($_POST['your_post_id']) ) { update_post_meta( $post->ID, 'your_post_id', $_POST['your_post_id'] );}
            }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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