Skip Navigation

[Resolved] Hot to get value of CF in a php script?

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 7 replies, has 2 voices.

Last updated by martinP-14 3 years, 10 months ago.

Assisted by: Jamal.

Author
Posts
#1958459

Hi,

I have a custom field group with a custom field with the slug "ots-notify-empfanger" which contains an e-mail address. Now I want to use this address in a script. The script works fine and gets triggered properly. I just cannot get the value of the custom field.

What I try:

function send_email_notification( $new_status, $old_status, $post ){
$post_ID=$post->ID;
$field_slug = "wpcf-ots-notify-empfanger";
$address = get_post_meta($post_ID, $field_slug, true);

But $address stays empty. Any idea what I do wrong?

#1959969

Hello and thank you for contacting the Toolset support.

From first sight, the code seems correct to me. Maybe $post does not actually hold the post object. Try to use error_log function to inspect what it has:

error_log( $post );
$post_ID=$post->ID;
error_log( $post_ID );
$field_slug = "wpcf-ots-notify-empfanger";
$address = get_post_meta($post_ID, $field_slug, true);
error_log( $address );

hidden link

The code seems to be hooked to the transition_post_status action, right?
https://developer.wordpress.org/reference/hooks/transition_post_status/
Can you share the full code?

#1965329

Dear Jamal,

thanks. I will send you the fill code of the function so that you have a better picture. The $post_id contains the id of the post and all other line do work. One mail to the author gets send, but not the one to the address from the custom field as the custom field does not get grabbed.

function send_email_notification( $new_status, $old_status, $post ){

$post_ID=$post->ID;

$postType=get_post_type($post_ID);

if($postType=="post" or $postType=="cb-ots-post"){

if ( 'publish' !== $new_status )
return;

if('publish' != $old_status){

$meta_values = get_post_meta($post_ID, 'is_notified', true);

if($meta_values!='yes'){

$pub_post = get_post($post_ID);
$author_id=$pub_post->post_author;
$post_title=$pub_post->post_title;
$postperma=get_permalink( $post_ID );
$user_info = get_userdata($author_id);

$usernameauth=$user_info->user_login;
$user_nicename=$user_info->user_nicename;
$user_email=$user_info->user_email;
$first_name=$user_info->user_firstname;
$last_name=$user_info->user_lastname;

$blog_title = get_bloginfo('name');
$siteurl=get_bloginfo('wpurl');
$siteurlhtml="$siteurl";

// Toolset: gran CF with address
$to_cf_post = get_post_meta($post_ID, 'wpcf-ots-notify-empfanger', true);

$publish_post_notification_settings=get_option('publish_post_notification_settings');

$subject=$publish_post_notification_settings['subject'];
$from_name=$publish_post_notification_settings['from_name'];
$from_email=$publish_post_notification_settings['from_email'];
$emailBody=$publish_post_notification_settings['emailBody'];

$emailBody=stripslashes($emailBody);
$emailBody=stripslashes(htmlspecialchars_decode($emailBody));

$charSet=get_bloginfo( "charset" );
$mailheaders='';
$mailheaders .= "Content-Type: text/html; charset=\"$charSet\"\n";
$mailheaders .= "From: $from_name <$from_email>" . "\r\n";
$emailBody=wpautop($emailBody);
$emailBody='<!DOCTYPE html><html '.get_language_attributes().'><head> <meta http-equiv="Content-Type" content="text/html; charset='. get_bloginfo( "charset" ).'" /><title>'.get_bloginfo( 'name', 'display' ).'</title></head><body>'.$emailBody
.'<br>'
.'<br>Post_ID: ' .$post_ID
.'<br>to_email: ' .$to_email
.'<br>to_cf_post: ' .$to_cf_post
.'</body></html>';

// Mail to author of post: works
$Rreturns=wp_mail($user_email, $subject, $emailBody, $mailheaders);
if($Rreturns){
add_post_meta($post_ID, 'is_notified', 'yes');
}

//2nd Mail to address from Custom Field: does not work as no value got grabbed
if ($to_cf_post != "" ) {
$Rreturns=wp_mail($to_cf_post, $subject, $emailBody, $mailheaders);
}
}
}
}
}

#1967105

Hallo? Anybody out there!

#1967301

HI, I tested the output of:

print_r ( get_post_meta ( $post_id ) );
print_r ( get_post_meta ( $post_id , 'wpcf-ots-absendermail' , false ) );
print_r ( get_post_meta ( $post_id , 'wpcf-ots-absendermail' , true ) );

And got in the first case:
... [wpcf-ots-notify-empfanger] => Array ( [0] => null@test.com ) ...

In the second:
[wpcf-ots-absendermail] => Array ( [0] => )

And in the third:
nothing...

Any idea how to use the get_post_meta correctly to get the value?

#1967779

Hello and my apologies for the late reply, but I do not work on Wednesdays and Thursdays.

According to this:
And got in the first case:
... [wpcf-ots-notify-empfanger] => Array ( [0] => null@test.com ) ...

You should be able to get the first instance of the field using:

get_post_meta ( $post_id , 'wpcf-ots-notify-empfanger' , true ) 

And an array of the emails in the field using:

get_post_meta ( $post_id , 'wpcf-ots-notify-empfanger' , false ) 

Note that you have used $post_ID in the full function's code, instead of $post_id with lower case 'id'. And the field meta_key is wpcf-ots-notify-empfanger instead of wpcf-ots-absendermail

Can you add a screenshot of the custom field definition for "ots-notify-empfanger"?

#1967947
Gruppe_bearbeiten_‹_Childhood_Business_—_WordPress.png

Hi Jamal,

thanks for your reply. Sorry for the mixture of "$post_id" and "$post_ID". The 1st is a code I used in a shortcode to show up the meta fields of a post in a post view. Otherwise, I always have to create a new post as the original code (with $post_ID) is for sending out an e-mail after publishing. So, this is not the source of error.

I also use two fields for testing: wpcf-ots-absendermail and wpcf-ots-notify-empfanger as one of them allows just one, the other multiple instances.

CF definitions see attached.

#1967987

My issue is resolved now.

All works fine now after debugging led to the error. It is that the original code "$post_ID=$post->ID;" does not deliver the right ID of the post somehow.

With get_the_ID() all works as intended. So: $meta = get_post_meta (get_the_ID(), 'wpcf-...' , true ); is working.

Thanks for your help and patience!