向CPT帖子添加自定义ID仅创建而不是更新

时间:2021-06-30 作者:Manuel Hermida

我正在尝试仅在创建帖子时自动填充自定义字段,而不是在更新帖子时自动填充。

CPT“;valoraciones“;ACF自定义字段“;ID\\u valoraciones;。

ID\\u valoraciones是一个递增的数字。也就是说,对于post 1,ID\\u valoraciones:1,post 2,ID\\u valoraciones:2等等。。。

到目前为止,我采取了以下行动:

           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 );
每次我更新帖子时,它都会修改;id\\u valoraciones“;,是否有任何方法使其在创建后不会修改该值?

1 个回复
最合适的回答,由SO网友:Marek 整理而成

是的,很简单-检查是否存在id_valoraciones 保存帖子时的元值。如果post没有id_valoraciones 设定、完成你的工作,获得最后的价值并激励它。如果岗位有id_valoraciones 梅塔,什么都不做。像这样:

<?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 );
顺便说一下,remove_action 你的部分代码是无用的,update_post_meta 不会触发save_post 钩子,当wp_insert_post()ing内部save_post

相关推荐