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"";
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Yes, there is.
These are some WP functions to get, add and update post meta values:
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 );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0