0

I have the following code in my functions.php, which executes a script upon publish:

function save_new_post($post_ID)
{

    $site_root = '/home/forexmag/public_html/directory2';
    $post = get_post($post_ID);
    $title = $post->post_title;
    $breaking_news = false;

    $categories = get_the_category($post_ID);
    if (is_array($categories) && !empty($categories))
    {
        foreach ($categories as $cat_det)
        {
            //this is the id for the breaking (bad) news
            if (5668 == $cat_det->cat_ID)
            {
                $breaking_news = true;
                break;
            }
        }
    }

    $exec_code = "/usr/local/bin/php 
            $site_root 
            /crons/cron_notify.php '$title' $post_ID 2 " . intval($breaking_news);
    exec(
        $exec_code
    );

}
add_action('publish_page', 'save_new_post', 10, 1);

When I publish a new post I keep getting the following error:

Catchable fatal error: Object of class WP_Post could not be converted to string in /home/forexmag/public_html/wp-content/themes/forex_magnates/functions.php on line 712

This line is where I set the $exec_code var. However, as you can see, and as I confirmed via var_dump, the $title variable is a string.

What am I missing?

4
  • 3
    Perhaps the problem is $post_ID is an WP_Post not an int.
    – user27457
    Feb 21 '13 at 17:59
  • Nope. I've confirmed that it is an int
    – Matanya
    Feb 21 '13 at 18:01
  • 1
    I don't know why your output looks right, because, as vancoder pointed out, "draft_to_publish" definitely provides a post object and not an ID. wordpress.org/support/topic/…
    – Jake
    Feb 21 '13 at 18:51
  • my bad, I was testing with publish_page. Edited accordingly. Thanks all
    – Matanya
    Feb 21 '13 at 19:03
2

Magenta is actually correct. $post_ID is a WP_Post object. Your exec code should effectively be using $post_ID->ID.

function save_new_post($post_ID)
{
    print_r($post_ID);
    die();

Returns

WP_Post Object ( [ID] => ...
8
  • 1
    He showed the output of $post_ID, I don't understand why you are so sure.
    – Jake
    Feb 21 '13 at 18:20
  • 3
    I'm so sure because I tested it, and I read the codex
    – vancoder
    Feb 21 '13 at 18:34
  • 1
    I elaborated on my answer accordingly.
    – vancoder
    Feb 21 '13 at 18:42
  • I see. He is using a set function as part of an existing action. He is definitely using $post_ID wrong in that case. Gotcha. I wasn't paying attention to that action at the bottom...
    – Jake
    Feb 21 '13 at 18:46
  • What I still don't understand is the claim:What's even weirder is that when I var_dump $exec_code and kill the script, I get a perfect string string(121) "/usr/local/bin/php /home/forexmag/public_html/directory2 /crons/cron_notify.php '2011 Free Free Forex Report' 9934 2 0" where 9934 is presumably the post id. Since class WP_Post has no __toString method. How can this be?
    – user27457
    Feb 21 '13 at 18:51

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.