2

I've only experienced this a few times: I have a plugin with many meta fields for pages and other post types. Very occasionally, the update_post_meta() function won't have any effect with just one field. the other fields work fine. This is after the field has worked several times but it just stops. If I go into my metaboxes.php code and change the field name (the meta_key) they start working again. What would cause this kind of "freezing up" behavior for postmeta?

Here's an example of the save function that I use:

in metaboxes.php:

function fws_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'fws_nonce' ] ) && wp_verify_nonce( $_POST[ 'fws_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

    //list of fields to save *this is connected to another feature that handles post revisions and isn't related
    $revision_field_array = Array(
        'featured',
        "type",
        "resource_type",
        "logo_header",
    ....more fields
    );

    foreach($revision_field_array as $field){
        if( isset( $_POST[ $field ] ) ) {
            update_post_meta( $post_id, $field, $_POST[ $field ] );
        }
    }
} add_action( 'save_post', 'fws_meta_save' );

So this save function works for all the fields except occasionally one field stops saving. When I click update in the page edit screen, It just shows the same value as before. I have debugged by doing something like this - for example the logo_header field stopped saving:

foreach($revision_field_array as $field){
    if( isset( $_POST[ $field ] ) ) {

        if($field == "logo_header"){
            error_log("old value: " . (string)get_post_meta( $post_id, $field, true )[0]);
            error_log("new value: " . $_POST[ $field ] );
        }

        update_post_meta( $post_id, $field, $_POST[ $field ] );

        if($field == "logo_header"){
            error_log("updated value: " . (string)get_post_meta( $post_id, $field, true )[0]);
        } );

    }
}

After attempted save, the log reads: [17-Feb-2019 19:36:36 UTC] old value: off [17-Feb-2019 19:36:36 UTC] new value: on [17-Feb-2019 19:36:36 UTC] updated value: off

so I can see the post contains the new value but it's not getting saved.

Some context: This is one blog on a multisite network with 20+ sites, running on WP Engine. PHP version 7.0 WP 5.0.3

I have added the following line to .htaccess to increase the max_input_vars in the post

php_value max_input_vars 10000
0

The server cache of WP engine can bother a little, they have a very invasive cache. Try removing the cache from the server:

https://wpengine.com/support/cache/

For debugging you could print the result of

update_post_meta ($ post_id, $ field, $ _POST [$ field]);

(which will be true or false) Because get_post_meta sometimes does not work since In fact, when you query a custom field, all the fields for that post are retrieved from database, cached via object cache, and subsequent requests pull data from cache and not from db.

It can also be a problem of priorities, try this:

add_action ('save_post', 'fws_meta_save', 20, 1);
  • Thanks @federico sgoifo! I added the priority (20) and the number of arguments (1) to the action and that did the trick. I also cleared the WP Engine cache. I'm curious to see if it would make sense to clear the meta cache programmatically. I've seen posts about doing this related to wp_query but not on save post – Chris Chalmers Feb 17 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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