Not showing values setted by update_post_meta()

266 views
Skip to first unread message

Jepser Bernardino

unread,
Mar 23, 2012, 6:53:06 PM3/23/12
to magic-...@googlegroups.com
Hi!
I'm using MF 2.0. I have a form that at the end, creates a new post (from post_type companies), an new user and then the functions add several custom fields, they are inserted into the post but when I load the page (in backend) the boxes aren't populated with their values, instead when you save the post, it overwrite the values.

Here is the code:
function new_startup(){
if(isset($_REQUEST['create']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'create')) {
echo '<pre><br/><br/><br/><br/><br/>';
print_r($_REQUEST);
echo '</pre>';
//cheking if user exists
$user_id = username_exists( $_REQUEST['hs-username'] );
if ( !$user_id ) {
$user_id = wp_create_user($_REQUEST['hs-username'], $_REQUEST['hs-password'], $_REQUEST['hs-email']);
$user_meta = array(
'ID' => $user_id,
'first_name' => $_REQUEST['hs-name'],
'role' => 'author'
);
wp_update_user($user_meta);
} else {
$random_password = __('User already exists.  Password inherited.','hs');
}
//creating the new company
$new_company = array(
'post_title' => $_REQUEST['hs-company'],
'post_type' => 'companies',
'post_content' => __('Welcome ','hs') . $_REQUEST['hs-name'] . __('. Write a brief of your company.','hs'),
'post_author' => $user_id,
'post_status' => 'publish'
);
$new_company_id = wp_insert_post($new_company);
wp_set_post_terms($new_company_id, array($_REQUEST['industry']), 'industry');
//setting questions into meta fields
if(isset($_REQUEST['hs-twitter'])) {
update_post_meta($new_company_id, 'general_twitter', $_REQUEST['hs-twitter']);
}
if(update_post_meta($new_company_id, 'questions_q', $_REQUEST['q']));
echo '<pre>';
print_r(get_post_meta($new_company_id, 'questions_q'));
echo '</pre>';
if(update_post_meta($new_company_id, 'questions_a', $_REQUEST['a']));
echo '<pre>';
print_r(get_post_meta($new_company_id, 'questions_a'));
echo '</pre>';
echo ' yeah ' . $new_company_id;
}
} //new_startup
add_action('init', 'new_startup');

Seth Stevenson

unread,
Apr 9, 2012, 12:50:12 PM4/9/12
to magic-...@googlegroups.com
I was trying to do something similar today. I found that with MF you can't just add values to wp_postmeta. MF also requires you update the wp_mf_post_meta table with the "field_count" and "group_count". My code below might help you, even though it's very specific to my use.

[code]
$current_user = wp_get_current_user();
// Create WP post, my custom post type is called 'profiles'
$my_post = array(
     'post_title' => $dname,
     'post_content' => '',
     'post_status' => 'draft',
'post_type' => 'profiles',
     'post_author' => $current_user->ID
  );
  // Create array of all the custom fields in my Magic Fields custom post type
  $post_metas = array('profile_username', 'profile_display_in', 'first_name', 'middle_name', 'last_name', 'credentials', 'education', 'certifications', 'profile_specialty', 'phone', 'fax', 'email', 'custom_title', 'position_position', 'position_group', 'position_link', 'position_hidden', 'research_year', 'research_description', 'research_hidden', 'publication_year', 'publication', 'publication_link', 'clinic_specialty', 'clinic_language', 'awards_and_honor_year', 'awards_and_honor_award');
// Insert the post into the database
  $the_post_id = wp_insert_post( $my_post );
  // Function I used to get the meta_id of the custom field
  function get_mid_by_key( $post_id, $meta_key ) {
 global $wpdb;
 $mid = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key) );
 if( $mid != '' )
return (int)$mid;
 
 return false;
}

        // For each custom field in my Magic Fields custom post type
foreach($post_metas as $post_meta){
add_post_meta($the_post_id, $post_meta, ''); // This creates a blank custom_field entry to start
$meta_id = get_mid_by_key( $the_post_id, $post_meta ); // Get the ID of that blank field we just inserted
// Now update mf_post_meta with a field_count and group_count of 1 to let magic fields know that the custom field has values, if you have more than one of the same custom field you'll have to adjust this
$wpdb->insert( 
'wp_mf_post_meta', 
array( 
'meta_id' => $meta_id,
'field_name' => $post_meta, 
'field_count' => 1,
'group_count' => 1,
'post_id' => $the_post_id
)
);
}

 finally update the custom fields with the real value, I get my values from a GET request
update_post_meta($the_post_id, 'profile_username', $_GET['uname']);
update_post_meta($the_post_id, 'first_name', $_GET['fname']);
update_post_meta($the_post_id, 'last_name', $_GET['lname']);  
update_post_meta($the_post_id, 'phone', $_GET['phone']);  
update_post_meta($the_post_id, 'email', $_GET['email']);  
update_post_meta($the_post_id, 'position_position', $position);  
update_post_meta($the_post_id, 'position_group', $group);
[/code]

Seth Stevenson

unread,
Apr 9, 2012, 12:53:06 PM4/9/12
to magic-...@googlegroups.com
The important part you probably need is:

$wpdb->insert( 
'wp_mf_post_meta', 
array( 
'meta_id' => $meta_id,
'field_name' => $post_meta, 
'field_count' => 1,
'group_count' => 1,
'post_id' => $the_post_id
)
);

Thomas Nelson

unread,
Apr 15, 2013, 1:43:09 PM4/15/13
to magic-...@googlegroups.com
This is exactly what I was looking for except the $wpdb->insert('wp_mf_post_meta' isnt working for me, the other call to the database to grab the meta_id works though, any idea why?
Reply all
Reply to author
Forward
0 new messages