I want to update the wp_postmeta table in the database based on 2 keys, is there a way to do that using any of the wordpress default functions. This is my DB query which is working fine:
$sql = "UPDATE wp_postmeta SET meta_value = meta_value + 1 WHERE post_id = 167788 AND meta_key = \"tie_views\"";
I want to update the wp_postmeta table in the database based on 2 keys, is there a way to do that using any of the wordpress default functions. This is my DB query which is working fine:
$sql = "UPDATE wp_postmeta SET meta_value = meta_value + 1 WHERE post_id = 167788 AND meta_key = \"tie_views\"";
Share
Improve this question
asked Aug 18, 2020 at 6:40
H_altH_alt
31 bronze badge
1 Answer
Reset to default 0Yes, there is.
These are some WP functions to get, add and update post meta values:
get_post_meta()
add_post_meta()
update_post_meta()
Your code could be:
$meta_value = (int) get_post_meta( $post->ID, 'meta_key', true );
update_post_meta( $post->ID, 'meta_key', $meta_value + 1 );