【问题标题】:wp_insert_post Only Once for Custom Post Typewp_insert_post 仅用于自定义帖子类型一次
【发布时间】:2013-04-11 12:12:14
【问题描述】:

我创建了一个启用自定义帖子类型的插件,并使用 wp_insert_post 为该帖子类型插入了一些默认值。

wp_insert_post( array(
    'ID' => '3',
    'post_status' => 'publish',
    'post_type' => 'exhibitor',
    'post_title' => 'Title',
    'post_content' => 'Description...'
) );

问题是每次页面刷新时都会重新插入我的值。我无法编辑或删除它们。

当我的插件被激活时,如何让 Wordpress 只更新自定义帖子类型?每次我刷新页面时,帖子都会再次发布。所以我无法编辑或删除帖子。

问候,

【问题讨论】:

    标签: wordpress plugins custom-post-type


    【解决方案1】:

    在您的插件中使用 register_activation_hook,即插件激活时要运行的插件函数。

    register_activation_hook(__FILE__, 'newplugin_install');
    function newplugin_install() {
    wp_insert_post( array(
        'ID' => '3',
        'post_status' => 'publish',
        'post_type' => 'exhibitor',
        'post_title' => 'Title',
        'post_content' => 'Description...'
    ) );
    
    }
    

    希望这对你有用;)

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 2016-09-11
      相关资源
      最近更新 更多