• Resolved Cezar Ayran

    (@ayrancd)


    I’d like to run some code when a new post is added or delete from the back-end of my custom post type… what action hook can I use to test if the post added/delete is from my custom post type and then do what I am looking for?

    Tks in advance!!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator threadi

    (@threadi)

    Thread Starter Cezar Ayran

    (@ayrancd)

    Awesome!! Tks a lot @threadi

    Thread Starter Cezar Ayran

    (@ayrancd)

    Solved

    Thread Starter Cezar Ayran

    (@ayrancd)

    @threadi I’m back again 🙂

    So I added the following code but nothing I’m not getting any change, it is like the action is not being executed

    do_action( "save_post", 'saveBlockTime', 10, 2);
    function saveBlockTime($post_id, $post ) {
    	/*if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE){
    		return;
    	}*/
    
    	update_post_meta(7143, 'trip', 150);
    
    	$message = "hoursss ".get_field('flight_hours', $post_id);
    
    	wp_mail("email@gmail.com", "flight", $message );
    }
    
    do_action( 'wp_trash_post', 'saveBlockTime', 10);

    FYI I’m adding this code to functions.php

    Thread Starter Cezar Ayran

    (@ayrancd)

    uhh just found out I had to change do_ with add_

    add_action( "publish_block-time", 'saveBlockTime', 20, 2);
    
    Thread Starter Cezar Ayran

    (@ayrancd)

    @threadi now I have a new issue, when I use this code

    add_action( "publish_block-time", 'saveBlockTime', 10, 2);

    I can print the post ID but I can’t print any meta_data it seems that it is being called before the meta is saved, same thing happens if I use save_post… do u know what action I have to use or priority to get the meta_data when a post is published?

    Thread Starter Cezar Ayran

    (@ayrancd)

    Just an update about the publish post I got here and I’ll create another topic about delete it.

    add_action( "publish_block-time", 'saveBlockTime', 10, 2);
    function saveBlockTime($post_id, $post ) {
    	$pilot = $_POST['acf']["field_641868dba4ea7"];
    
    	$getCurrentHours = get_field('block_time', 'user_'.$pilot);
    	$calculateBlockTime = intval($getCurrentHours)-intval($_POST['acf']["field_641867f757fe8"]);
    
    	update_user_meta($pilot, 'block_time', $calculateBlockTime);
    }
    Moderator threadi

    (@threadi)

    do u know what action I have to use or priority to get the meta_data when a post is published?

    Which meta do you mean? postmeta? Its saved before ‘save_post’-hook is called. You did use another hook.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Add and delete post action’ is closed to new replies.