Home / custom field / Auto create custom field
  • How to Auto create custom field

    no thumbnail sorry: Auto create custom field

    Adding this PHP code to your functions.php of your wordpress theme will auto create a custom field with a default value. In this example the custom field is sort order,

    wordpress snippet : PHP

    //Set Default Meta Value
    function set_default_meta($post_ID){
    	$current_field_value = get_post_meta($post_ID,'Sort Order',true);
    	$default_meta = '100'; // value
    	if ($current_field_value == '' && !wp_is_post_revision($post_ID)){
    		add_post_meta($post_ID,'Sort Order',$default_meta,true);
    	return $post_ID;

    ( WordPress codex functions, hooks, in this snippet. )

    get_post, is_post, wp_insert_post, add_post_meta, get_post_meta, wp_is_post_revision, add_action, wp,