Add custom ID to CPT posts only create not update

I’m trying to autofill a custom field only when the post is created and not when it is updated.

CPT “valoraciones”
ACF custom fields “ID_valoraciones”.

The ID_valoraciones is an incremental number. That is, for post 1, ID_valoraciones: 1, post 2, ID_valoraciones: 2 and so on …

So far I have the following action:

           function auto_assign_ids( $post_id, $post, $update ) {

        // Only assign ID to new approved "valoraciones" posts
        if ( $post->post_status == 'publish' && $post->post_type == 'valoraciones' ) {
       
            // get the most recent "valoraciones" posts
            $valoracion_args = array(
                'numberposts'       =>   2,
                'post_type'         =>   'valoraciones',
                'orderby'           =>   'post_date',
                'order'             =>   'DESC'
            );
            $valoraciones = get_posts( $valoracion_args );
       
            // get the id_valoraciones of the prior post
            $last_id = get_post_meta( $valoraciones[1]->ID, 'id_valoraciones', true );
       
            // increment
            if ( !$last_id ) {
                $last_id = 0;
            }       
             $last_id++;
            
                // unhook this function so it doesn't loop infinitely
                remove_action( 'save_post', 'auto_assign_ids', 10, 3 );
                // set the id_valoraciones of the current post
                update_post_meta( $post_id, 'id_valoraciones', $last_id );
                // re-hook this function
                add_action( 'save_post', 'auto_assign_ids', 10, 3 );
        }
        
 
}
add_action( 'save_post', 'auto_assign_ids', 10, 3 );

Every time I update a post, it modifies the value of the “id_valoraciones”, is there any way so that once it is created it does not modify that value?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Yes, it’s simple – check for existence of id_valoraciones meta value when saving post. If post does not have id_valoraciones set, do your job with getting last value and incerementing it. If the post has id_valoraciones meta, then – do nothing. Like this:

<?php
function auto_assign_ids( $post_id, $post, $update ) {

    // Only assign ID to new approved "valoraciones" posts
    if ( $post->post_status == 'publish' && $post->post_type == 'valoraciones' ) {
                
        $id_valoraciones = get_post_meta( $post_id, 'id_valoraciones', true );
        
        if( ! $id_valoraciones ) {
            
            // get the most recent "valoraciones" posts
            $valoracion_args = array(
            'numberposts'       =>   2,
            'post_type'         =>   'valoraciones',
            'orderby'           =>   'post_date',
            'order'             =>   'DESC'
            );
            $valoraciones = get_posts( $valoracion_args );

            // get the id_valoraciones of the prior post
            $last_id = get_post_meta( $valoraciones[1]->ID, 'id_valoraciones', true );

            // increment
            if ( !$last_id ) {
                $last_id = 0;
            }       
            
            $last_id++;
            
            update_post_meta( $post_id, 'id_valoraciones', $last_id );
            
        } else {
            
            // id_valoraciones was set before
            
        }
        
    }
}
add_action( 'save_post', 'auto_assign_ids', 100, 3 );

By the way, remove_action part of your code is useless, update_post_meta does not trigger save_post hook, it’s used when wp_insert_post()ing inside save_post hook.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x