I have managed to fill custom fields with certain predefined values of all my post, but the changes don't take effect until I manually update the posts.
Example:
// Predefined variable and value
$page_description = "This is a sample value that will be put (echo $page_description) into a custom field";
// Echo Variable into Custom Field
echo '<input type="text" name="custom-field-name" value="'.$page_description.'" />';
The above example works as I expected. It inserts the $page_description value into the custom field; so when I go to edit that post I will see the variable value. The issue is that in the current edit screen, without updating yet, if I look at the post on the front end of the site, that custom field has not been updated/saved with that variable value, even though I see in the edit screen. It is not until I update the post that the variable value is saved for good. I have 1,800+ posts that need to be updated. I need a solution to update them.
Someone suggested using ajax to auto-update all existing posts so that the new custom field values are saved to the posts. I read an article about it, but not sure how to do it. Or if there is a plugin out there that I can use. I would like some assistance with this.
Thanks for any input on this!
UDPATE 6-23-11:
This is the coding I inserting into my theme's custom field file:
$postauth = get_user_meta($post->post_author, 'nickname', true);
/********** UPDATE ALL POST FIELDS Function **********/
//check if data exists
if ($postauth){
//if it exists just echo it out
echo $postauth;
}else{
//if not , update the post meta with your default value and the echo it out.
$update_author = get_user_meta($post->post_author, 'nickname', true);
update_post_meta($post->ID,'nickname',$update_author);
echo $update_author;
}
/********** End UPDATE ALL POST FIELDS Function **********/
I after inserting the code and adjusting my terms, I saved the file and refreshed my site. The code should update a custom field (Author) for all posts that belong to an author and save the author's nickname into that field. I have a basic search option separately installed that is set to search custom fields. So I tested if the update worked by searching for the nickname of a given author who has 8 posts published. Three of the eight were manually updated and they showed up in the custom field search I did for the author's nickname. Now the other 5 posts did not come up in the search results, which lets me know that all the posts were not updated. Are there any solutions to this?
UPDATE 6-24-11:
I have been searching everywhere in my theme to figure out where to place the solution provided by Bainternet, but no success. I am using the couponpress theme v6.1.4. I have placed the code below in: _header.php, _single.php, system_customfields.php.
//check if data exists. This is checking if the predefined field is empty or not.
if ($bizname_field != ''){
//if it exists just echo it out
echo 'existing value';
}else{
//if not , update the post meta with your default value and the echo it out.
// New Variable Value
$newvalue = 'New value here';
update_post_meta($post->ID,'custom-field-name',$newvalue);
echo $newvalue;
}
I am going around in circles and would appreciate some help. Thanks a lot.