delete all postmeta from posts with key ‘pietje puk’

WordPress AI Code Robot

Create WordPress code snippets for free without a developer!

Aert requested the  following code from my WordPress AI Code Generator.

delete all postmeta from posts with key 'pietje puk'

The robot generated the following code:

You can use the below code to meet your requirements. Please replace 'YOUR_THEME_SLUG' with the slug of your theme or plugin in the function name to avoid potential conflicts.

“`php

add_action('init', 'YOUR_THEME_SLUG_nm_delete_postmeta');

function YOUR_THEME_SLUG_nm_delete_postmeta() {

$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'pietje puk'
);

$posts = get_posts($args);

foreach($posts as $post) {
delete_post_meta($post->ID, 'pietje puk');
}
}
“`

Just for information, this code snippet will find all posts that contain the custom field ('postmeta') with key 'pietje puk', and then delete that custom field for each post. But please be aware, it will be better if you use this within an admin action, like in response to a button click, because this will be run on every WordPress initial load (init hook), and it is not necessary if you need to do it just once.

More
CODE SNIPPETS