This function is in my functions.php file. The query returns the value I’m looking for via phpmyadmin, but only AFTER the function has been run in WordPress.
add_action( 'transition_post_status', 'a_new_post', 10, 3 );
function a_new_post( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status or 'publish' === $old_status )
return;
if ( 'post' == $post->jobman_job )
return; // as far as here, I'm just checking conditions and they're working fine.
global $wpdb;
$current_post_id = $post->ID; // this is fine... variable is getting set with the post id.
$table_name = $wpdb->prefix . 'postmeta'; // fine...
$result = $wpdb->get_var("SELECT meta_value FROM " . $table_name . " WHERE meta_key = 'data19' AND post_id = " .$current_post_id. " ORDER BY post_id DESC");
print_r($wpdb->last_query ); // After this function has run, when I put the last query into phpmyadmin, I get my result just fine.
var_dump ($result); // I'm getting NULL when I run the query in WordPress.
}
It seems that at the point in time when I run the function, the new meta_value I’m wanting to retrieve hasn’t been saved into the postmeta table yet.
Is there anyway to delay my query from running until the new meta_value is present? or maybe save the post_meta table before I run the query? this is killing me slowly. Any help would be great!