Tagged: 

Viewing 1 reply thread
  • Author
    Posts
    • #7833
      titidodo
      Participant

      Hi everybody,

      i’m using Piklist for a CPT and i want to change the permalink structure. Right now the post_name as part of the url is generated out of the post_title, but i want it to be generated out of a custom (piklist) field. I edited the permalink manually for existing posts, but when i save and then edit and update the post, the permalink is the old one generated from the post_title.

      Can you help me solving this problem?

      Thanks in advance!

    • #7835
      ccarey75
      Participant

      You want the save_post hook
      https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

      add_action( 'save_post', 'my_save_post', 11, 2 );
      
      function my_save_post($post_id, $post){
      
         //if it is just a revision don't worry about it
         if (wp_is_post_revision($post_id))
            return false;
      
         //if the post is an auto-draft we don't want to do anything either
         if($post->post_status != 'auto-draft' ){
      
            // unhook this function so it doesn't loop infinitely
            remove_action('save_post', 'my_save_post' );
      
            //this is where it happens -- update the post and change the post_name/slug to whatever you want
            wp_update_post(array('ID' => $post_id, 'post_name' => YOUR_SLUG_HERE );
      
            //re-hook this function
            add_action('save_post', 'my_save_post' );
         }
      }
Viewing 1 reply thread
  • You must be logged in to reply to this topic.