gpt4 book ai didi

php - 获取 “Object of class WP_Post could not be converted to string” - 当它是一个字符串时

转载 作者:可可西里 更新时间:2023-11-01 12:53:27 30 4
gpt4 key购买 nike

我的 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_code var 的地方。但是,如您所见,并且我通过 var_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 Answers 上获得了正确答案。认为我应该分享它:

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

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

返回

WP_Post Object ( [ID] => ...

vancoder 提供

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

关于php - 获取 “Object of class WP_Post could not be converted to string” - 当它是一个字符串时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15009005/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com