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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号