1

I need to change the slug of the attachment post. Currently, it is showing attachment name , I want it to be replaced by id of attachment.

Edit : Thanks to suggestion by TheDeadMedic, I am now seeing urls with their postname changed to id in url (slug of the url.). But somehow it is adding now adding /attachment/ as well to the url right before slug (id). How should I remove this /attachment/ part.

2

Use the add_attachment hook to update the slug after the attachment has been inserted:

function wpse_182454_attachment_id_as_slug( $post_id ) {
    if ( get_post_field( 'post_name', $post_id ) != $post_id ) {
        wp_update_post(
            array(
                'ID' => $post_id,
                'post_name' => ( string ) $post_id,
            )
        );
    }
}

add_action( 'add_attachment', 'wpse_182454_attachment_id_as_slug' );
  • thanks its working, but now I am getting /attachment/ before the post_name (which is id now) – akshayb Mar 27 '15 at 11:36
  • @akshayb, you asked how to replace the slug and you got an answer ;). If you need something else to happen as wel as replacing the slug then please edit the question and state it explicitly. – Mark Kaplun Mar 27 '15 at 12:51

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.