Current status: Solved. . Done
How to retrieve a custom field within the function.php?
  • I want to get the value of each post to send an email, when the post gets published. Unfortunately it does not work. :(

    Tried this both calls:
    ===========================
    $to = get_post_meta($post_id, 'recipient', true);
    $to = get_field('recipient', $post_id);
    ===========================


    The full code of the function:
    ===========================
    function wpr_Notification($post_id) {
    $post = get_post($post_id);
    //$to = get_post_meta($post_id, 'recipient', true);
    $to = get_field('recipient', $post_id);
    $headers = 'From: Disctopia Metal Webzine ' . "\r\n" ;
    $title = $post->post_title." has been published";
    $message = "
    We recently published or edited an article,...
    ";

    wp_mail($to, $title, $message, $headers);
    }
    add_action('publish_post', 'wpr_Notification');
    ===========================


    Have no clue, why it wont work...
  • Hi @Neotopia,

    Thanks for the bug report. I'll test this code and report my findings soon

    Cheers
    Elliot
  • Hi, has is it maybe something to do with trying it "outside the loop" - whatever it means? Have put the code into my function.php.
  • Hi @Neotopia,

    Outside the loop is fine if you put in the $post_id as you have. Can you make sure the $post_id is correct and that the data exists in the wp_postmeta table?


    Cheers
    Elliot
  • Hi Elliot,

    yes, I think so. Can find some of the custom fields in the postmeta table:
    (21553, 7446, 'recipient', 'neotopia@disctopia.de'),

    But what seems to be a little weird is:
    (21505, 7440, 'sub-genre_deathmetal', 'neotopia@disctopia.de'),

    "sub-genre_deathmetal" is never filled with an email adress. But still, post 7446 does not work nor post 7440. I am not able to reach the custom field. :(

    If I can send you any other information, just let me know. I would desperately do anything to get this work.
  • The issue with the false meta-keys is probably my fault. I have changed the name from "email recipient" to "recipient". All old values with meta-key "email recipient" got transfered to "sub-genre_deathmetal".

    Although, all values are in the data base and all new "recipient" are correct.
  • Hi Elliot,

    thanks for the new version. Tried 3.5.7.1, but it does still not work. :(
  • Hi @Neotopia,

    Can you clarify where you are up to with your issue? Have you tried debuging the problem? ie. not sending the email, just dumping out the get_field value?

    Perhaps you could go through the get_field function in core/api.php and dump out the variables throughout the function and find the line which is going wrong?

    Cheers
    Elliot
  • Hi Elliot :-)

    Uff, I am afraid your proposal is beyond my knowledge about PHP.

    I have tried it several times with get_field aswell as with get_post_meta. Tried also just to get the value in the body text in the email or at other positions. But I never was able to actually retrieve it. I changed the themes (testing on a subdomain of mine), experimented with other plugins (activating, uninstalling, ...) and always with the same result. Unfortunately, I am not able to debug the problem in detail as a lack of knowledge.

    I know, my problem seems pretty unique and mostly it's my own problem and not yours. Therefore I really appreciate your tryings to help me. If you like, you could get access to my configuration on the testing-installation. For me, I am out of options - my data base tells me about 200 entries where I tried to solve it.

    cheers,
    Martin
  • Hi @Neotopia,

    Sorry, I do not work as a web developer for free. This is a support forum for web developers to report ACF bugs / issues.

    I recommend that you hire a web developer to debug the issue.

    Good luck!

    Cheers
    Elliot
  • Hi Elliot,

    it wasn't meant that way. At least is this bug absolutely related to ACF, cause if I use the "normal" custom fields, everything works just fine.

    But thank you anyway for trying to help and fix the issue in the first place. :-)

    cheers,
    Martin