admin管理员组文章数量:1130349
When running the following code:
update_post_meta( 1, '_visibility', 'visible' );
Does wordpress check to see if post_id 1 key _visable has a value of visible before writing to database?
I have an import script and I'm wondering is this would save processing time.
function update_post_meta_check($post_id, $key, $value) {
$old = get_post_meta($post_id, $key);
if ($old == $value) {
return false;
} else {
return update_post_meta($post_id, $key, $value);
}
}
Or does Wordpress already do this check itself?
For reference I expect many of my post meta to remain unchanged - so the extra get_post_meta will be ok if i can bypas alot of update_post_meta's...
When running the following code:
update_post_meta( 1, '_visibility', 'visible' );
Does wordpress check to see if post_id 1 key _visable has a value of visible before writing to database?
I have an import script and I'm wondering is this would save processing time.
function update_post_meta_check($post_id, $key, $value) {
$old = get_post_meta($post_id, $key);
if ($old == $value) {
return false;
} else {
return update_post_meta($post_id, $key, $value);
}
}
Or does Wordpress already do this check itself?
For reference I expect many of my post meta to remain unchanged - so the extra get_post_meta will be ok if i can bypas alot of update_post_meta's...
1 Answer
Reset to default 2update_post_meta() uses update_metadata() to update the post metadata, and if you call update_post_meta() without specifying the fourth parameter (i.e. $prev_value) — or that the value is empty, then yes, update_metadata() will check if the new value is the same as the current value in the database, and if so, the metadata will not be updated.
You can check lines #195 to #202 in the update_metadata() source for the revelant code.
(Update: Added the note below; above the "Additional Note" section.)
And even if you specify the $prev_value (and set it to a non-empty value), MySQL will not update the metadata if $prev_value equals to the new value:
// Returns FALSE if current database value is 'hidden'...
update_post_meta( 123, '_visibility', 'hidden', 'hidden' );
Additional Note: Update metadata if exists; else, don't create new.
update_post_meta() / update_metadata() will automatically add the metadata if an existing entry is not found in the database. So if you don't want that to happen, you can use metadata_exists() to prevent the automatic metadata creation; for example:
// 123 is the post ID.
if ( metadata_exists( 'post', 123, '_visibility' ) ) {
update_post_meta( 123, '_visibility', 'hidden' );
}
When running the following code:
update_post_meta( 1, '_visibility', 'visible' );
Does wordpress check to see if post_id 1 key _visable has a value of visible before writing to database?
I have an import script and I'm wondering is this would save processing time.
function update_post_meta_check($post_id, $key, $value) {
$old = get_post_meta($post_id, $key);
if ($old == $value) {
return false;
} else {
return update_post_meta($post_id, $key, $value);
}
}
Or does Wordpress already do this check itself?
For reference I expect many of my post meta to remain unchanged - so the extra get_post_meta will be ok if i can bypas alot of update_post_meta's...
When running the following code:
update_post_meta( 1, '_visibility', 'visible' );
Does wordpress check to see if post_id 1 key _visable has a value of visible before writing to database?
I have an import script and I'm wondering is this would save processing time.
function update_post_meta_check($post_id, $key, $value) {
$old = get_post_meta($post_id, $key);
if ($old == $value) {
return false;
} else {
return update_post_meta($post_id, $key, $value);
}
}
Or does Wordpress already do this check itself?
For reference I expect many of my post meta to remain unchanged - so the extra get_post_meta will be ok if i can bypas alot of update_post_meta's...
1 Answer
Reset to default 2update_post_meta() uses update_metadata() to update the post metadata, and if you call update_post_meta() without specifying the fourth parameter (i.e. $prev_value) — or that the value is empty, then yes, update_metadata() will check if the new value is the same as the current value in the database, and if so, the metadata will not be updated.
You can check lines #195 to #202 in the update_metadata() source for the revelant code.
(Update: Added the note below; above the "Additional Note" section.)
And even if you specify the $prev_value (and set it to a non-empty value), MySQL will not update the metadata if $prev_value equals to the new value:
// Returns FALSE if current database value is 'hidden'...
update_post_meta( 123, '_visibility', 'hidden', 'hidden' );
Additional Note: Update metadata if exists; else, don't create new.
update_post_meta() / update_metadata() will automatically add the metadata if an existing entry is not found in the database. So if you don't want that to happen, you can use metadata_exists() to prevent the automatic metadata creation; for example:
// 123 is the post ID.
if ( metadata_exists( 'post', 123, '_visibility' ) ) {
update_post_meta( 123, '_visibility', 'hidden' );
}
本文标签: post metaDoes quotupdatepostmetaquot check if value is the same before updating
版权声明:本文标题:post meta - Does "update_post_meta" check if value is the same before updating? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749205575a2332485.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论