【问题标题】:Move Wordpress Custom Post Type Content to a Custom Field Value将 Wordpress 自定义帖子类型内容移动到自定义字段值
【发布时间】:2013-11-14 01:22:28
【问题描述】:

大家好,我有 163 个帖子从 csv 文件导入到自定义帖子类型中,除了一个小问题外,一切都很好。该插件迫使我将一列放入内容字段 - 所以我选择了一个我将作为自定义字段放入的列 - 我现在是否可以将它们全部转换为自定义字段并清除内容区域?

谢谢!

【问题讨论】:

    标签: mysql sql database wordpress


    【解决方案1】:

    您可以在functions.php 中添加这样的内容(未经测试)

    add_action( 'get_header', 'so19967768_update_posts' );
    function so19967768_update_posts()
    {
        if( ! isset( $_GET['update'] ) )
            return;
    
        $posts = get_posts( array( 'post_type' => 'my_post_type', 'posts_per_page' => -1 ) );
    
        foreach( $posts as $post )
        {
            setup_postdata( $post );
            $value = $post->post_content;
            update_post_meta( $post->ID, 'my_meta_key', $value );
            wp_update_post( array(
                'ID' => $post->ID,
                'post_content' => ''
            ) );
        }
    
        echo 'update complete!';
    
        die();
    }
    

    并访问yoursite.com/?update=1 运行更新

    【讨论】:

    • 我确实获得了成功的“更新完成!”消息,但是该自定义帖子类型的内容并未移动到自定义字段并从内容中删除。
    • 刚刚运行了一个测试,它可以工作。您是否将上面代码中的my_post_typemy_meta_key 更改为所需的值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2014-06-25
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多