• Hi,

    How can I extract the post meta in add_action publish_post? As far as I know, the meta will only be saved later, so the meta is still empty in this hook.
    I’ve been researching the subject, but unfortunately none of the solutions led anywhere …

    Thank you very much ??

    add_action( 'publish_post', 'add_post_notification', 10, 7 );
    function add_post_notification( $post_ID, $post, $update = false) {
    
    	if ( $update ||  $post->post_date != $post->post_modified ) {
    		return;
    	}
    	
    	if(get_post_meta($post, 'meta_name', true)) {
    
    		// do something...
    
    	} else {
    
    		// do something...
    	}
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @robertherold – Have you tried add_action('save_post', 'add_post_notification', 100, 2); ?

    Moderator bcworkz

    (@bcworkz)

    Use a different hook, the one that fires when the post meta is added or updated. There’s one for both before and after updating the DB. Depending on what you want to do, one or the other would be more appropriate.
    “update_postmeta”
    “updated_postmeta”

    Thread Starter robertherold

    (@robertherold)

    @hiyottaunits Unfortunately, the else branch is executed in the same way. ??

    Moderator bcworkz

    (@bcworkz)

    The last add_action() term ought to be either 2 or 3, but not 7. Trying to use 100 instead of 10 as the third hints at the issue, but fails to address it. The reason “save_post” cannot get post meta isn’t exactly because it’s saved later. The update_postmeta() call does happen before the “save_post” action, but writing to the DB is relatively slow, and PHP keeps executing while this happens. When “save_post” fires, the DB is not yet done writing, hence you get nothing. This is known in coding as a “race condition”.

    Thread Starter robertherold

    (@robertherold)

    @bcworkz Unfortunately, no solution is good. I can’t believe this…

    Moderator bcworkz

    (@bcworkz)

    Correct, there is no good solution via “save_post” related to meta data. Did you try “update_postmeta” and/or “updated_postmeta”? These should work for most post meta data, however it’s possible a plugin saves meta data through other means, such as a direct DB UPDATE query. A workable solution depends on what meta you are after and how it is actually saved.

    function map_update($post_id) {
    $ID=$post_id;
    //if ($post->post_type = 'post') return;
        $link = get_permalink ( $ID );
        $title = get_the_title ( $ID );
        $mapaddress = get_post_meta ( $ID, 'address', true );  
      global $wpdb;
    
        $table = $wpdb->prefix . 'wpgmza';
    
        $data = array(
                    'link' => $link,
                    'title' => $title,
                    'address' => $mapaddress,<code></code>
    
                );
    
        $wpdb->insert($table , $data);
    }
    add_action('publish_post', 'map_update' );
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘add_action publish_post get_post_meta’ is closed to new replies.