Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
your edit is not correct, re-read bainternet's code and compare. in your example, you're checking if the author has a nickname and if false, setting a custom field- to a non-existent nickname. you want to check if the custom field exists, not if the user has a nickname. –  Milo Jun 23 '11 at 0:45
    
Thank you Milo, I'll review the coding. Thanks for the input. –  user1893 Jun 23 '11 at 2:52
    
@Milo: You were right, it does work. But what I noticed is that I have even though I dont have to update each post, I have to open them in edit screen to take effect. Thanks! –  user1893 Jun 23 '11 at 3:37
add comment

1 Answer

up vote 1 down vote accepted

The problem is you are not "filling" the custom field with data you are just outputting the value in the edit screen, so you must save or update, and to have that data inside all of your posts you will need to either create a custom query of all the posts and update there meta or manually edit each post. Both options are bad when there are 1500 posts.

A simpler solution would be to update the data "on the fly" meaning that on your theme file (the one that displays the meta data) create a conditional check if that field exists and if not the update it, something like:

//get saved data
$page_description = get_post_meta($post->ID,'custom-field-name',true);
//check if data exists
if ($page_description){
//if it exists just echo it out
   echo $page_description;
}else{
//if not , update the post meta with your default value and the echo it out.
   $default_value = 'This is a sample value that will be put (echo $page_description) into a custom field';
   update_post_meta($post->ID,'custom-field-name',$default_value);
   echo $default_value;
}
share|improve this answer
    
@Bainternet: I tried your solution, but it didn't work. I'll update my post. –  user1893 Jun 22 '11 at 23:21
    
@Bainternet: Sorry, I made in a mistake with the code that you gave me. Let me rework it. Thanks a lot. –  user1893 Jun 23 '11 at 2:53
    
@Bainternet: Yes, it works, but what I notice is that although I dont have to update the each post; I still have to open them in edit screen for the changes to apply on the front end of the site. Do you know of a way I can initiate this for all posts? –  user1893 Jun 23 '11 at 3:35
    
If you put this code on your theme file you don't have to go to the edit screen for each post. –  Bainternet Jun 23 '11 at 4:44
    
Sorry, but do you mean the theme's functions.php, single.php, header.php. Im not sure where Im not sure what file you're referring to. –  user1893 Jun 23 '11 at 5:33
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.