Skip to content

Instantly share code, notes, and snippets.

@ekka21
Created July 11, 2013 20:36
Show Gist options
  • Save ekka21/5979014 to your computer and use it in GitHub Desktop.
Save ekka21/5979014 to your computer and use it in GitHub Desktop.
Wordpress: add custom meta key to all of the post type
<?php
#domain.com/?run_script=1 to run this script
function add_my_custom_meta_key(){
global $wpdb;
if ($_GET['run_script'] =='1' && !is_admin() )
{
$q = 'SELECT id from wp_posts where post_type = "MY_CUSTOM_POST_TYPE"';
$ids = $wpdb->get_col($q);
foreach($ids as $id)
{
$arr = array('post_id' => $id, 'meta_key' => 'MY_KEY_NAME', 'meta_value' => 'MY_VALUE');
$wpdb->insert( 'wp_postmeta', $arr );
}
}
print 'DONE!';
}
add_action( 'init', 'add_my_custom_meta_key' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment