admin管理员组

文章数量:1122832

When saving a post that is a custom post type (in the example, 'my_cpt'), I'm trying to check if a specific piece of metadata exists for that post. I'm using this exact same code on another site (different hosting environment) and it works fine, but in my current use case it's throwing a warning in the debugger. I've stripped this down to the minimum code required to cause the warning while demonstrating the function and its conditions. If I comment out the "if(metadata_exists(...))" statement, there is no warning; so I'm confident that is the source. I've also tested without the nonce in place, and the warning still occurs. While I know warnings aren't errors, I'd like to understand what the debugger thinks I'm doing wrong here, and learn if there's a better way to accomplish this goal.

The code:

function save_my_cpt_mb_fields($zmmodelid) {
    if (wp_verify_nonce($_REQUEST['zmcpt_nonce'], 'zmcpt_nonce_val')) {
        if (metadata_exists('my_cpt', $zmmodelid, '_zmbaseprice')) {
            delete_post_meta($zmmodelid, '_zmbaseprice');
        }   
    } else {
        return;
    }
}
add_action('save_post_my_cpt', 'save_my_cpt_mb_fields');

The warning: "Warning: Trying to access array offset on value of type bool in /home/server-name/public_html/wp-includes/meta.php on line 757"

When saving a post that is a custom post type (in the example, 'my_cpt'), I'm trying to check if a specific piece of metadata exists for that post. I'm using this exact same code on another site (different hosting environment) and it works fine, but in my current use case it's throwing a warning in the debugger. I've stripped this down to the minimum code required to cause the warning while demonstrating the function and its conditions. If I comment out the "if(metadata_exists(...))" statement, there is no warning; so I'm confident that is the source. I've also tested without the nonce in place, and the warning still occurs. While I know warnings aren't errors, I'd like to understand what the debugger thinks I'm doing wrong here, and learn if there's a better way to accomplish this goal.

The code:

function save_my_cpt_mb_fields($zmmodelid) {
    if (wp_verify_nonce($_REQUEST['zmcpt_nonce'], 'zmcpt_nonce_val')) {
        if (metadata_exists('my_cpt', $zmmodelid, '_zmbaseprice')) {
            delete_post_meta($zmmodelid, '_zmbaseprice');
        }   
    } else {
        return;
    }
}
add_action('save_post_my_cpt', 'save_my_cpt_mb_fields');

The warning: "Warning: Trying to access array offset on value of type bool in /home/server-name/public_html/wp-includes/meta.php on line 757"

Share Improve this question asked Jul 13, 2024 at 21:57 Scott HScott H 313 bronze badges 2
  • if I understand the documentation, you should use post as first argument of metadata_exists : developer.wordpress.org/reference/functions/metadata_exists – mmm Commented Jul 14, 2024 at 1:46
  • Thanks mmm - that was it. I clearly confused myself somewhere between the other project I referenced and this one. I appreciate you setting me straight! – Scott H Commented Jul 14, 2024 at 5:40
Add a comment  | 

1 Answer 1

Reset to default 2

Thanks to mmm's comment above, it was pointed out that even for CPTs the first parameter's value needs to be "post", as clearly stated in the documentation at https://developer.wordpress.org/reference/functions/metadata_exists/.

Somewhere along the way I confused myself into thinking that the first parameter's value needed to be the CPT handle for a custom post type (CPT), which was NOT correct. Thanks again mmm, and apologies for my oversight.

本文标签: