繁体   English   中英

wp_insert_post运行多次

[英]wp_insert_post running multiple times

我对WordPress的开发不是很熟悉,基本上我有一个单独的PHP脚本,我无法将表单数据从它传递给WP插件,并且插件将通过$ _GET左右获取该数据并将其插入WP作为新职位。

我已经在可以正常工作的插件中尝试了以下代码,但是会导致有关“调用未定义的函数is_user_loggedin”的错误。

$post = array(
            'post_title' => "test title",
            'post_content' => "test content",
            'post_date' => 0,
            'post_date_gmt' => 0,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_category' => array(1)
    );
wp_insert_post($post);

然后,我找到了解决方案,将代码包装在函数中,然后使用“ init”钩子执行该代码。

function insert_post(){
    $post = array(
            'post_title' => "test title",
            'post_content' => "test content",
            'post_date' => 0,
            'post_date_gmt' => 0,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_category' => array(1)
    );
    wp_insert_post($post);
}
add_action('init', 'insert_post');

同样,这会插入帖子,但它会不断地不断重复插入同一帖子,而不是在调用我的插件时只插入一次。

任何帮助或解决方案,将不胜感激,谢谢。

我不使用Wordpress。 但是当我看WordPress WP插入后指导

您不应将代码包装在Function中。 请重新检查是否已包含所有必需的包含/必需文件才能调用wp_insert

例如:

// Gather post data.
$my_post = array(
    'post_title'    => 'My post',
    'post_content'  => 'This is my post.',
    'post_status'   => 'publish',
    'post_author'   => 1,
    'post_category' => array( 8,39 )
);

// Insert the post into the database.
wp_insert_post( $my_post );

wp_insert_post()通过sanitize_post()传递数据,后者本身处理所有必要的清除和验证(kses等)。

根据WordPress的指导

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM