• DenisCGN

    (@deniscgn)


    Hello everybody,

    I cant get my function work. I searched some threats, and they say that it wont work.

    I just want to transfer a custom field to post_excerpt if the post is saved or updated.

    This is the code I found and tested, but it only works if I save the Post 2 times.

    function my_function( $post_id ){
    	
    	// Only set for post_type = post!
    	/*if ( 'citadela-item' !== $post->post_type ) {
    		return;
    	}*/
    	
        if ( ! wp_is_post_revision( $post_id ) ){
    		
     		// unhook this function so it doesn't loop infinitely
            remove_action('save_post', 'my_function');
    		
    		$address_to_excerpt = get_post_meta( $post_id, '_city_address', true );
         
            // update the post, which calls save_post again
            $my_args = array(
    			'ID'           => $post_id,
    			'post_excerpt' => $address_to_excerpt,
    		);
            wp_update_post( $my_args );
     
            // re-hook this function
            add_action('save_post', 'my_function');
        }
    }
    add_action( 'save_post', 'my_function' );

    Now I found this ACtion wp_after_insert_post but I dont understand how it works. there are no examples I understand. But it seems to be the right action.

    Any suggestions to help?

    Thanks,
    Denis

    • This topic was modified 4 years ago by DenisCGN.
  • The topic ‘Save_Post & get_post_meta’ is closed to new replies.