0

I'm trying to update the slug for all the post within a specific custom post type, let's say "music".

Here is the code:

/* Update slug with the latest post title */
function auto_update_post_slug($postId, $after, $before) {
    if ($after->post_title != $before->post_title) {
        $after->post_name = '';
        wp_update_post($after);
    }
}

/* Check if custom post type music, if true, add action to auto update slug */
function enqueue_music_features( $hook_suffix ){
    $my_custom_post_type = 'music';

    if( in_array($hook_suffix, array('post.php', 'post-new.php') ) ){
        $screen = get_current_screen();

    if( is_object( $screen ) && $my_custom_post_type == $screen->post_type ){

        /* add custom post type music features */
        add_action('post_updated', 'auto_update_post_slug', 10, 3);

        echo "<script type='text/javascript'>alert('This has been called');</script>";

        }
    }
}
add_action( 'admin_enqueue_scripts', 'enqueue_music_features');

What I do know that the enqueue_music_features only calls the alert in the custom post type music, and that auto_update_post_slug works by itself if you put it outside enqueue_music_features.

I also tried putting the entire auto_update_post_slug inside the hook, that didn't work either.

The main issue is that I'm not able to combine these two parts, even if they work on by themselves.

  • Or, if trying to update pre-existing posts en masse: wordpress.stackexchange.com/questions/46086/… (Of course modifying either solution to address your custom post type). Also note this answer regarding slug sanitization if setting the slug to something other than an empty string. – bosco Mar 3 '18 at 22:08
  • I looked at some of the other questions, but the issue I'm facing is when I'm combining the hook and the "update slug". @your second comment, I will check that one out, it looks similar to what I've trying to achieve! – Dom Mar 3 '18 at 22:51
1

With the help and direction of bosco I was able to get it right! The code below is a modified version of the answer found here.

function my_enqueue_music_features( $hook_suffix ){


$posts = get_posts( array (  
    'numberposts' => -1, //this will get all
    'post_type'   => 'music'
) );

foreach ( $posts as $post ) {
    // check the slug and run an update if necessary 
    $new_slug = sanitize_title( $post->post_title );
    if ( $post->post_name != $new_slug )
    {
        wp_update_post(
            array (
                'ID'        => $post->ID,
                'post_name' => $new_slug
            )
        );
    }
}

}
add_action( 'admin_enqueue_scripts', 'my_enqueue_music_features');

The only thing I added was basically this: 'post_type' => 'music'. Change "music" to your custom post type.

| improve this answer | |
  • Glad you got it hashed out :) . The only thing you might want to be careful of is leaving this hook attached - it will update every music post on every admin dash page load, which will slow things down eventually. Be sure to comment out the add_action() when you don't need it! – bosco Mar 4 '18 at 1:28
  • @bosco For this specific custom post type I will need this running at all time, is there any way I can optimize it? The only thing I can think of is to change the numberpost to only the most recent (e.g.) 10 posts. I was mainly looking for something that would only update the post that I'm currently working on. – Dom Mar 4 '18 at 5:27

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