I used an import plug-in (WP All Import) to import data from an old website into WordPress. The importer placed all data into ACF fields as was intended and this process worked correctly. . But for some reason, the data for these ACF fields was also placed into the content field below the title in the WP admin backend.. While the template reads from the ACF fields, and not the content field, I don’t want this data to remain.
Can a PHP function be written that would cycle through all pages of a given post type and remove all data from the content field. After execution, that function could be removed, as it would no longer be needed.
I am aware that I could load each page individually, remove the data and save, but I imported over 1000 records and that would just take too long. I’m looking for this process to be automated.
I’ve written some functions.php scripts before, but for this one, I have no idea where to start or knowledge if such a thing is even possible. Ideas?
Easiest is with an sql query. You want delete content from post where post type is what ever post type you want. If you don’t have database access, use wp_query in php.
Here’s what I did when I had a similar requirement (Adding bulk data instead of removing).
Made a shortcode in functions.php that loops through all the posts, for each post, clear its content like this:
`foreach ($all_posts as $post) {`
`$post_data = array(`
`’ID’ => $post->ID,`
`’post_content’ => ”,`
`);`
`wp_update_post($post_data);`
`}`
In order to use the shortcode, add it on any page to run it.
SQL: `UPDATE wp_postmeta SET meta_value = NULL WHERE meta_key = ‘[custom field name]’`
OR
`DELETE FROM wp_postmeta WHERE meta_key = ‘[custom field name]’`
I assume that WP CLI is the perfect tool for such operation.
Here is one sample command. You have to update your post type name. Also try it on a local server or staging site & then only in live. Additionally keep a backup of your site before doing such activities.
wp post list –post_type=your_post_type –field=ID | xargs -I % wp post update % –post_content=””
Can your script be modified so that I can define which post-type the script with apply to? I don’t want this to apply to regular pages or posts. Only to a custom post-type I created called ’archices’. If I remember correctly, the main content field for a page is known as ‘the_content’. Your script uses page_content. Is this the same field?