Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取“;类WP_Post的对象无法转换为字符串”;-当它是字符串时_Php_Wordpress - Fatal编程技术网

Php 获取“;类WP_Post的对象无法转换为字符串”;-当它是字符串时

Php 获取“;类WP_Post的对象无法转换为字符串”;-当它是字符串时,php,wordpress,Php,Wordpress,我的functions.php中有以下代码,它在发布时执行脚本: 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); i

我的functions.php中有以下代码,它在发布时执行脚本:

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('draft_to_publish', 'save_new_post', 10, 1);
当我发布一篇新文章时,我不断遇到以下错误:

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
这一行是我设置
$exec\u code
变量的地方。但是,正如您所看到的,并且正如我通过var\u dump确认的那样,
$title
变量是一个字符串。我错过了什么

编辑:更奇怪的是,当我var_dump
$exec_code
并杀死脚本时,我得到了一个完美的字符串:

string(121) "/usr/local/bin/php /home/forexmag/public_html/directory2/crons/cron_notify.php '2011 Free Forex Report' 9934 2 0"

得到了Wordpress答案的正确答案。我想我应该分享它:

$post\u ID
是一个
WP\u post
对象。您的exec代码应该有效地使用
$post\u ID->ID.

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

WP_Post Object ( [ID] => ...
承蒙


请注意,这取决于您使用的挂钩。例如,如果你使用
publish\u page
你得到了一个整数(这实际上是我困惑的根源)

我得到了同样的错误,但是我的问题最终得到了解决

<?php wp_reset_query(); ?>

这是因为正在使用WP_查询,并且正在中断页面后面的其余查询


我希望这对某人有所帮助

有一天,我的网站上也出现了同样的错误,但代码不是问题所在。问题在于数据库。我所做的是进入我的数据库并更改siteurl和home,因为里面有胡言乱语。我认为我的网站被破坏了,所以我更改了数据库密码。此网站将帮助您摆脱可捕获的致命错误


我试过了,相信我成功了。

你能确认
$post\u ID
是字符串吗?它是整数。但是连接时不应该转换成字符串吗?@Jrod,你说得对。我使用publish_页面钩子进行了测试,它给出了一个int,而draft_to_publish one使用了一个对象