doubeiji2602 2019-06-11 19:39
浏览 99
已采纳

Wordpress | 批量将内容从主编辑器移动到自定义字段

I've used the Advanced Custom Fields plugin to create a new Custom Field in Wordpress. My aim now is to move all Download Links from "the_content", to the newly created Custom Field "the_field('download_link')". The issue is that I have over 10,000 posts to modify. I was wondering if there is a quick way of doing this, rather than manually moving the download link for each post?

Please see the images below for an idea of what I am trying to achieve.

Before | After

One hurdle is that all content is saved in the "wp_posts" table, where the custom field content is saved in the "wp_postmeta" table.

The content saved in the "download_link" custom field, looks like this in the "wp_postmeta" table:

(8214, 2282, 'download_link', '<div class=\"source\"><a href=\"https://www.google.com/\" target=\"_blank\"><img src=\"https://www.google.com/image.png\"></a></div>'),
(8215, 2282, '_download_link', 'field_5cffd35335ce3'),
(8220, 2280, 'download_link', '<div class=\"source\"><a href=\"https://www.google.com/\" target=\"_blank\"><img src=\"https://www.google.com/image.png\"></a></div>'),
(8221, 2280, '_download_link', 'field_5cffd35335ce3'),
(8226, 2278, 'download_link', '<div class=\"source\"><a href=\"https://www.google.com/\" target=\"_blank\"><img src=\"https://www.google.com/image.png\"></a></div>'),
(8227, 2278, '_download_link', 'field_5cffd35335ce3'),

Can this be done at all? Or is the only real way to achieve this is by moving the download links manually?

Thanks in advance for your help.

  • 写回答

1条回答 默认 最新

  • douxie9471 2019-06-11 21:27
    关注

    It can be done automatically if the format of your download link is always the same.

    1. Backup your database

    2. Create a php file named my-cleanup.php. Loop over all products, grab the link from the description and move it to a post_meta.

    3. Launch the file with WP-CLI: wp eval-file my-cleanup.php

    Sample code

    // Select the posts you want
    $args = array(
        'post_type'      => 'post',
        'post_status'    => 'any',
    
        // -1 means all posts
        // For debug, set this to a small number
        'posts_per_page' => 3,
    );
    
    // Retrieve posts
    $query = new WP_Query( $args );
    $posts = $query->posts;
    
    // Pattern to match
    // [wpdm_package id='90228']
    $pattern = "/(\[wpdm_package id='\d+'\])/";
    
    // Process posts one by one
    foreach ( $posts as $post ) {
        echo "Processing " . $post->post_title . "
    ";
        $post_args['ID'] = $post->ID;
    
        // Get the shortcode from the post content
        preg_match( $pattern, $post->post_content, $matches );
    
        // Do we have a match?
        if ( count( $matches) > 0 ) {
    
            // Retrieve shortcode
            $shortcode = $matches[0];
    
            // Convert shortcode, maybe add some HTML
            $link = do_shortcode( $shortcode );
    
            // remove shortcode from description
            $post_args['post_content'] = preg_replace( $pattern, '', $post->post_content );
    
            // Update post
            wp_update_post( $post_args );
    
            // Create the post_metas
            add_post_meta( $post->ID, '_download_link', 'field_5cffd35335ce3', true );
            add_post_meta( $post->ID, 'download_link',  $link, true );
        }
    }
    

    Notes

    1. Backup your database

    2. Test on a few posts to get confidence in your code. You can restore the previous revision in the editor.

    3. Then try on 100, then 1000. You will often find small differences in the way posts are entered.

    4. This might take hours to run on 10.000 posts, which is why you need a WP-CLI command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图