Enter WP_Post
and the “magic” getter – did you know…
$value = get_post_meta( $post->ID, 'custom_field', true );
…is the same as
$value = $post->custom_field;
So you can shorten your code to:
update_post_meta( $post->ID, 'custom_field', $post->custom_field + 1 );
Note you cannot do $post->custom_field++
– since the property doesn’t actually exist, it can’t be modified. Read more about overloading in PHP.
Related Posts:
- How to update custom fields using the wp_insert_post() function?
- How to display multiple Post meta_key/meta_values by SQL query
- How to use multiple query with same meta key
- How can update custom meta for all posts
- Use ajax to update_post_meta
- How can I create a list of page titles from custom meta values?
- Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
- How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
- Is it possible to retrieve all posts with a certain value for metadata?
- Non-Closing PHP Query in WordPress Loop
- how to get serialized post meta
- WordPress loop by meta key that is an array? and how loop multiple arrays
- How to store multiple custom meta box
- Order a WP_Query by meta value where the value is an array
- Add a custom class to the body tag using custom fields
- Define category ID using get_post_meta
- Hide a div when a custom field is empty
- WordPress stripping out custom field tags
- Order category posts by last word of custom field
- Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
- How to update custom fields when post is published?
- WP post meta – for loop inside for loop
- When working with a post, almost all wp_postmeta are deleted
- Updating Lat and Lng of posts automatically gives sporadic results
- If custom field doesn’t exist, or exists and it’s true show title?
- Metadata on the WP_Post object
- Adding custom input for data attribute in insert media modal
- Display all meta for a post?
- Custom Meta Field – Youtube embed
- Print html when custom field has value inside while loop
- Update WordPress Custom Field with AJAX on cached page
- How to Explode a Textarea Field and Echo each line separately, wrapped with HTML
- How to Display Image Meta underneath EVERY image in EVERY post
- Randomly Assign an Image’s Alt Text Based on Data From Post
- Passing the custom field values in the wp_get_current_user array function
- Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
- Custom profile field with birthday. Troubles with
- Firing schema via code in functions.php doesn’t work
- auto-populating custom nav with all items from custom post type
- Automatic Shortcode Creation with Custom Fields [closed]
- Custom field values to taxonomy terms
- How would an if statement surrounding a custom field with two variables (holding values) look like?
- How to check if a PHP string is different than meta field?
- Display custom field on 404 page outside loop
- Querying Database with wpdb
- update_post_meta only updating on last loop of foreach
- Only show image from custom field when present
- Hide Heading if ACF Field is empty
- Custom meta box values are not getting saved for my custom post type
- WP_Query: getting posts where custom field exists
- How to show meta value code HTML after x paragraph
- Creating an image from a custom field
- User Meta Value not echoing despite Var_Dump Showing correct string
- How to add post meta in while loop?
- Add category to post meta
- Problem with adding custom CSS class to image in ACF Photo Gallery plugin [closed]
- Output custom text field as unordered list
- Unable to render custom field after attempt to generate a list of recent post in page template
- Unable to Call More than One Meta Box Output
- Conditional On custom field plugin metabox
- Filter unique custom field value based on custom taxonomy category or other custom field
- Meta boxes only displayed when editing normal (default wp post_type) posts
- Using an “IF” statement based on the existence of custom field
- Woocommerce Custom Meta Boxes- How to only display if they actually have content
- Updating Metadata with Shortcode
- How do you save the values from custom fields to a products data
- Is it possible to update the dataset using update_post_meta
- Update Images after edit via php
- Hide a div if the fiels is empty
- Setting up a cron job to auto update a custom field
- No more get_option(‘date_time’) in WordPress 5.5?
- Delete post meta by serialized meta value
- Show a text if the post has written by author user role in WordPress
- Trying to retrieve meta values from three different meta keys and display in rows
- Reverse Cross-Sells (WooCommerce)
- Store custom field’s multiple values in one user meta key
- Get field in readable word
- WooCommerce multiple custom fields code
- Post Meta statement?
- WP_query meta_query slow with OR and DATES
- Get meta_query value by user meta array
- meta field for numerous links
- WordPress AJAX load post metadata in modal
- Include a custom field in mysql query
- Display Content Based on Custom Field Value
- get_post_meta with variable
- divide custom field values in div every two values
- Showing custom field contents without listing description
- How to exclude post from being queried based on custom meta value
- How to link a word comprised of a custom field with another?
- Custom field in title
- Posts sortable column not sorting properly for custom field numbers
- How to add together (get the sum) a field from all author’s posts
- Search result page – display values from the result’s metaboxes
- ACF Date fileds to Age Convert [closed]
- Save custom field value to variable for conditional – why is this not working? [closed]
- Displaying custom meta box value in a custom post page
- How do you create a front end form that enables the editing of member-specific custom fields in WordPress?
- Search custom post type posts only by meta fields?
- How to pass value to add_filter wpcf7_form_tag from another function?