0

First I tried fetching post_meta data, and latest, I tried fetching get_post_field() inside a transition_post_status hooked function, like:

<?php
add_action( 'transition_post_status', 'yes_send_email_notification', 10, 3 );
function yes_send_email_notification( $new_status, $old_status, $post ) {

    if( 'mycpt' === $post->post_type && 'new' === $old_status && 'pending' === $new_status ) :
        $post_id = $post->ID;
        /**
         * Generate Dynamic values
         */
        $post_view_link   = get_permalink( $post_id );

        $message = "My custom message<br>";
        $message .= "Link: ". esc_url( $post_view_link );

        //headers are duely passed
        //$headers = ...

        wp_mail( 'someone@example.com', 'New MyCPT submitted', $message, $headers );

    endif;
}

The email is working fine. Now I tried including post_content, post_title (and if possible, post_meta data) into the email content, inside the function, but unfortunately, I can't get any of 'em.

$post_id        = $post->ID;
$post_view_link = get_permalink( $post_id );
$post_title     = get_post_field( 'post_title', $post, 'display' );
$post_content   = get_post_field( 'post_content', $post, 'display' );
$post_excerpt   = wp_trim_words( $post_content, 70, null );

But I'm failing. $post_title and $post_excerpt aren't getting populated. (The same happened for post_meta fields)

How can I get post_fields (and if possible post_meta fields too) inside a transition_post_status hooked function?

6
  • Just guessing, but could it be that the function get_post_field is not available at this point and you have to access the datastructure directly, like you do with the ID?
    – cjbj
    Aug 23 '16 at 18:03
  • you have to access the datastructure directly, like you do with the ID - You mean a custom $wpdb query? Aug 24 '16 at 13:43
  • No, just $post->post_title and post->post_content
    – cjbj
    Aug 25 '16 at 10:02
  • @cjbj I found $post->post_content is working fine, but unfortunately the $post->post_title isn't working. I guess the $post object is not complete. :( Aug 26 '16 at 13:20
  • Have you tried a var_dump on $post?
    – cjbj
    Aug 26 '16 at 13:28
1

I'm not sure why get_post_field wouldn't work in this situation, but if you can access $post->ID, you should also be able to get $post->post_title and $post->post_content (and even $post->post_excerpt if it's there).

The $post object includes the standard metadata, like author and publication date. Any metadata in the wp_postmeta table in the database can only be found with $post->ID, with which you should be able to get your metadata using get_metadata. Failing that you can always run a query on the database directly. Since plugins have quite some freedom in structuring their metadata it's not possible in general to say how to structure this query. Also: read Otto's note on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.