Products
GG网络技术分享 2025-03-18 16:12 3
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.
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.
It can be done automatically if the format of your download link is always the same.
Backup your database
Create a php file named my-cleanup.php
.
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
Backup your database
Test on a few posts to get confidence in your code. You can restore the previous revision in the editor.
Then try on 100, then 1000. You will often find small differences in the way posts are entered.
This might take hours to run on 10.000 posts, which is why you need a WP-CLI command.
Demand feedback