Created a plugin for posting to a discord. Separate record type, with its own dopics. I tested the output of fields in a loop (the output of all fields in archive-cta.php) is output. the plugin does not work. That is - post fasting, but doppolya does not pull. get_the_title ($ postid) pulled out without problems.

function cta_send_notification( $new_status, $old_status, $post ) { $sxss=get_post_type(); if( 'publish' === $new_status && 'publish' !== $old_status && $sxss == 'cta' ) { function postToDiscord($message) { $data = array( "content" => $message, "username" => "CTA PING BOT", ); $curl = curl_init("https://discordapp.com/api/webhooks/idchannel/key"); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); return curl_exec($curl); curl_close($curl); } $postid =get_the_ID(); $taste=get_the_title($postid); global $post; // get post meta $pstmt1=get_post_meta( $postid, 'wpcf-fleetformat', true ); $message="**"; $message.=$taste; $message.=' ** ``` -------------------------------------------------------------- Location: UALX-3 Datetime: 10.08.18 20:00 ET FleetFormat: '.$pstmt1.' FC: TBD -------------------------------------------------------------- ```'; postToDiscord($message); } } add_action( 'transition_post_status', 'cta_send_notification', 10, 3 ); 

I suspect that the fields are written in the database after publication. and they still do not get into the database, and I try to get them

 function cta_send_notification( $post_id ) { if ( wp_is_post_revision( $post_id ) || get_post($post_id)->post_status != 'publish' ) { return; } function postToDiscord($message){ $data = array( "content" => $message, "username" => "CTA PING BOT", ); $curl = curl_init("https://discordapp.com/api/webhooks/айди/кей"); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); return curl_exec($curl); curl_close($curl); }; $postid =get_the_ID(); $taste=get_the_title($postid); // get post meta $pstmt1=get_post_meta($postid, 'wpcf-date-time', true ); $message="**"; $message.=$taste; $message.=' ** ``` ------------------------------ ID '.$postid.' Title '.$taste.' post meta : '.$pstmt1.' ------------------------------ ```'; postToDiscord($message); } add_action( 'save_post_cta', 'cta_send_notification', 99999, 1 ); 

Remade under save_post. The result is the same. Aidi and title normally pulls. Doppol does not pull

  • Why transition_post_status? There is a save_post event. - KAGG Design
  • transition_post_status in order to send to the discord only at the first post. save_post will always work "An event is triggered whenever a post (post, page) is created or updated, including when posting via import, xmlrpc or email." - Igor Fostyak
  • Did you read my answer? - KAGG Design
  • @KAGGDesign yes, thanks for the reply! Now I will redo it through save_post. I also think that it first sends standard fields to the database (title, text of the post), then changes the status to publish, and then throws the dopol into the database. therefore, they are empty for me ( - Igor Fostyak
  • @KAGGDesign remade under save_post, the same rake (the code in the question from the top added. Filter works for the record type. Pulls the id and title. And all .... the dopol field is not available anymore - Igor Fostyak

1 answer 1

The problem, most likely, is that the wpcf-fleetformat updated on the save_post event. This is an ordinary action.

Try hanging your cta_send_notification() on save_post . The title will look like this:

 function cta_send_notification( $post_ID, $post, $update ) { 

where arguments are: post id, post itself, bool is an update of an existing post or not.

And declaring your hook should use a lower priority (so that the hook is executed later), for example

 add_action( 'save_post', 'cta_send_notification', 999999, 3 );