我写了一个插入帖子的函数,但由于某种原因它不起作用。
function insertPost($title, $content, $status, $category = '')
{
$postData = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => $status,
'post_author' => 1,
'post_category' => array( 8,39 )
);
return wp_insert_post(wp_slash($postData));
}
以前可以这样插入,现在有点麻烦。
WP日志:
[18-Nov-2020 12:12:01 UTC] PHP Notice: Trying to get property 'feeds' of non-object in /storage/ssd1/253/15325253/public_html/wp-includes/post.php on line 4482
[18-Nov-2020 12:12:01 UTC] PHP Notice: Trying to get property 'use_trailing_slashes' of non-object in /storage/ssd1/253/15325253/public_html/wp-includes/link-template.php on line 49
[18-Nov-2020 12:12:01 UTC] PHP Fatal error: Uncaught Error: Call to undefined function is_user_logged_in() in /storage/ssd1/253/15325253/public_html/wp-includes/post.php:2611
Stack trace:
#0 /storage/ssd1/253/15325253/public_html/wp-includes/post.php(6994): _count_posts_cache_key('post', 'readable')
#1 /storage/ssd1/253/15325253/public_html/wp-includes/class-wp-hook.php(287): _transition_post_status('origin', 'new', Object(WP_Post))
#2 /storage/ssd1/253/15325253/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array)
#3 /storage/ssd1/253/15325253/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#4 /storage/ssd1/253/15325253/public_html/wp-includes/post.php(4813): do_action('transition_post...', 'origin', 'new', Object(WP_Post))
#5 /storage/ssd1/253/15325253/public_html/wp-includes/post.php(4160): wp_transition_post_status('origin', 'new', Object(WP_Post))
#6 /storage/ssd1/253/15325253/public_html/wp-content/plugins/T2W/includes/class-plugin-name-loader.php(153): wp_insert_post(Array)
#7 /storage/ssd1/253/1532 in /storage/ssd1/253/15325253/public_html/wp-includes/post.php on line 2611
该函数未定义,因为内核尚未完成加载。你想打电话太快了
wp_insert_post
。pluggable.php
包含,的文件is_user_logged_in
在加载所有插件后立即连接,之后plugins_loaded
.将您的代码挂在钩子上
plugins_loaded
或init
(甚至稍后触发),并且不要尝试直接在插件文件中执行它。