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 |1 Answer
Reset to default 2Thanks 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.
本文标签:
版权声明:本文标题:save post - Why would I get a "Trying to access array offset on value of type bool" warning when using &qu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300943a1931004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
post
as first argument ofmetadata_exists
: developer.wordpress.org/reference/functions/metadata_exists – mmm Commented Jul 14, 2024 at 1:46