Ok, so I'm working on a WordPress plugin. When the plugin is activated I want it to scan through the current posts and look to see if they contain at least one of two meta_keys (custom fields). Let's call them "key1" and "key2".
If it finds either of those meta_keys I want it to add (or update) a new meta_key and set it's value to "1". Let's call it "master_key".
I've tried using the register_activation_hook and instaniating a loop but I can't seem to get it to work. My knowledge of the loop and its inner workings is fairly limited. Can anyone take a look and see what could be fixed? Many thanks gang. Here's the code I'm working with:
function myplugin_activate(){
add_filter('posts_join', 'myplugin_activatefunc');
remove_filter('posts_join', 'myplugin_activatefunc');
}
function myplugin_activatefunc(){
$pageposts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id", OBJECT);
if ($pageposts): foreach ($pageposts as $post): $post_id = $post->ID;
$key1 = get_post_meta($post_id, 'key1', $single = true);
$key2 = get_post_meta($post_id, 'key2', $single = true);
if ((isset($key1)) && (!empty($key1))) {
delete_post_meta($post_id, 'master_key');
add_post_meta($post_id, 'master_key', '1');
}
elseif ((isset($key2)) && (!empty($key2))) {
delete_post_meta($post_id, 'master_key');
add_post_meta($post_id, 'master_key', '1');
}
endforeach;
endif;
}
register_activation_hook( __FILE__, 'mpm_activate' );