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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15009005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:21:56  来源:igfitidea点击:

Getting “Object of class WP_Post could not be converted to string” - when it is a string

phpwordpress

提问by Matanya

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

我的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);

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_codevar. However, as you can see, and as I confirmed via var_dump, the $titlevariable is a string. What am I missing?

这一行是我设置$exec_codevar 的地方。但是,正如您所看到的,正如我通过 var_dump 确认的那样,该$title变量是一个字符串。我错过了什么?

EDIT: What's even weirder is that when I var_dump $exec_codeand kill the script, I get a perfect string:

编辑:更奇怪的是,当我 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"

回答by Matanya

Got the correct answer on Wordpress Answers. Thought I should share it:

在 Wordpress Answers 上得到了正确答案。认为我应该分享它:

$post_IDis a WP_Postobject. Your exec code should effectively be using $post_ID->ID.

$post_ID是一个WP_Post对象。您的执行代码应该有效地使用$post_ID->ID.

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

Returns

退货

WP_Post Object ( [ID] => ...

Courtesy of vancoder

vancoder 提供

Do notice that this depends on the hook you are using. E.g if you use publish_pageyou get an integer (This was actually the source of my confusion)

请注意,这取决于您使用的钩子。例如,如果你使用publish_page你会得到一个整数(这实际上是我困惑的根源)

回答by ConorJohn

I was getting the same error, however the solution to my problem in the end was

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

<?php wp_reset_query(); ?>

This was because WP_Query was being used and was interrupting the rest of the queries further down the page.

这是因为正在使用 WP_Query 并且正在中断页面下方的其余查询。

I hope this helps someone

我希望这可以帮助别人

回答by Wayne Umrah

I got that same error on my website one day but the code wasn't the problem. The problem lies in the database. What I did was go into my database and change the siteurl and home because there was gibberish inside it. I think my website was compromise so I change the database password. This website will help you to get rid of the catchable fatal error https://www.msgdigital.com/catchable-fatal-error-object-of-class-wp_error-could-not-be-converted-to-string/.

有一天我在我的网站上遇到了同样的错误,但代码不是问题。问题出在数据库上。我所做的是进入我的数据库并更改siteurl和home,因为里面有乱码。我认为我的网站受到了威胁,所以我更改了数据库密码。该网站将帮助您摆脱可捕获的致命错误https://www.msgdigital.com/catchable-fatal-error-object-of-class-wp_error-could-not-be-converted-to-string/

I tried it and trust me it worked.

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