0

I am attempting to get the title displayed for a function. I am sending myself an email to see if I can get this to work for different titles. Currently the only way I can get it to work is via hard coded number inside the get_the_title. my example below works.

$tweetingthing = get_the_title(1067);   

    $to      = 'myemail@email.com';
    $subject = 'Hi!';
    $body    =  'Hi,' . chr(10) . chr(10) . 'Hi title= ' .  $tweetingthing . ' page id is   ' . $this_page_id;


    if( wp_mail($to, $subject, $body ) ) {
        echo('<p>Message successfully sent!</p>');
    } else {
        echo('<p>Message delivery failed...</p>');
    }

I obviously want more than just that one title, I'd like each title to be sent via email. I have tried this:

global $post;
//$thePostID = $post->ID;
$wp_query->post;
$this_page_id = $post->ID;
echo $this_page_id ;
$tweetingthing = get_the_title($this_page_id);

    $to      = 'mh@gmail.com';
    $subject = 'Hi!';
    $body    =  'Hi,' . chr(10) . chr(10) . 'Hi title= ' . $tweetingthing . ' Post id is   ' . $this_page_id;


    if( wp_mail($to, $subject, $body ) ) {
        echo('<p>Message successfully sent!</p>');
    } else {
        echo('<p>Message delivery failed...</p>');
    }

This provides null for $tweetingthing and $this_page_id (I know it's a post id I'm trying to get I simply used the variable giving in the example hope that doesn't confuse anyone).

I also tried

$tweetingthing = get_the_title($post_id);

also tried $tweet_post_id = the_ID(); $tweetingthing = get_the_title($tweet_post_id);

no luck

$page_object = get_queried_object();
$tweetingthing = get_queried_object_id();

results in a post title of 0

Addition info:

I'm using a status transition action to make the function fire. When it goes to publish and matches the custom post type it performs.

I'm using a custom post type

How can I get Custom Post Title?

full source code :

function intercept_all_status_changes( $new_status, $old_status, $post ) {
 if ( $new_status == 'publish' && get_post_type( $post ) == 'tweet' ) {
     // Post status changed
     twitter_run_when_published ();
 }
}  
add_action( 'transition_post_status', 'intercept_all_status_changes', 10, 3 );


function twitter_run_when_published () {
//
//email the stuffuff
//

 global $post;
 global $wp_query;
 //$thePostID = $post->ID;
 $wp_query->post;
 $this_page_id = $post->ID;
 echo $this_page_id ;

    $to      = 'meanderingmonarch@gmail.com';
    $subject = 'Hi!';
    $body    =  'Hi,' . chr(10) . chr(10) . 'Hi title= ' . $tweetingthing . ' Post id is   ' . $this_page_id;


     if( wp_mail($to, $subject, $body ) ) {
         echo('<p>Message successfully sent!</p>');
     } else {
         echo('<p>Message delivery failed...</p>');
     }
}

So after @nickyoung mentioned this was weird I thought I'd try a if status changes at all send an email. It sent me 4 messages.

The first: title was not there (post status not there) The second: title was not their (post status not there) The third: title WAS there (and post status was draft) The fourth: no title and post status was not there

edit: just cleaned up some jibberish sentences

edit2: Added full source code to see if anything may help

edit3: Added extra info about post status transitions

  • Use $this_page_id as a paramater. – ahmetlutfu May 5 '14 at 19:38
  • @ahmetlutfu you mean in $tweetingthing = get_the_title($this_page_id); or is there another one? – Cody May 5 '14 at 20:18
  • What happens if you try using $post->post_title instead of calling get_the_title() ? – Nick Young May 5 '14 at 21:15
  • @NickYoung nothing, the email doesn't show the title at all. I have it setup as follows. global $post; and $tweetingthing = $post->post_title; – Cody May 5 '14 at 21:37
  • Hmm that is a really weird issue. What exact action/filter are you using to run this code? Edit: did not see edit, looking now. – Nick Young May 5 '14 at 21:38
0

If you do something like:

$custompost = get_post(get_the_ID());
echo $custompost->post_title;

You'll find it works. There's clearly a bug somewhere inside the Wordpress core - this is happening to me too for a custom post type. Not sure what triggers it, but the above is a workaround.

0

I think you're doing this overly complicated. You already have the according $post, so just pass its ID to your function.

Try it like so:

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

    if (
        $new_status === 'publish'
        && get_post_type( $post ) === 'tweet'
    ) {
        // Post status changed
        twitter_run_when_published( $post->ID );
    }
}
add_action( 'transition_post_status', 'intercept_all_status_changes', 10, 3 );

function twitter_run_when_published( $post_id ) {

    $title = get_the_title( $post_id )

    $to = 'meanderingmonarch@gmail.com';
    $subject = 'Hi!';
    $body = 'Title: ' . $title . ', ID: ' . $post_id;

    if ( wp_mail( $to, $subject, $body ) ) {
        echo '<p>Message successfully sent!</p>';
    } else {
        echo '<p>Message delivery failed...</p>';
    }
}

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.