You’re setting a value whether it’s checked or not with this line:
$chk = ( isset( $_POST['special_box_check'] ) && $_POST['special_box_check'] ) ? 'on' : 'off';
The value is going to be either on
or off
, so your if
condition is always true, since it always has a value.
You could change it to save only if it’s checked, or delete it if it’s not. That way your condition will work, and you won’t have extra meta entries just to save the off state.
if ( isset( $_POST['special_box_check'] ) && $_POST['special_box_check'] ) {
add_post_meta( $post_id, 'special_box_check', 'on', true );
} else {
delete_post_meta( $post_id, 'special_box_check' );
}
Related Posts:
- How to update custom fields using the wp_insert_post() function?
- Most efficient way to add javascript file to specific post and/or pages?
- Display all values of custom a field created with ACF on a page
- Adding custom input for data attribute in insert media modal
- If Custom Field is empty don’t display div
- 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
- get_post_custom single array
- how to increase custom post value by one most efficiently?
- Passing the custom field values in the wp_get_current_user array function
- Custom profile field with birthday. Troubles with
- wordpress allow user to edit user profile with custom fields
- How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
- 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 combine 2 php functions into one function with a custom field
- Saving zero in meta box
- How to check if a PHP string is different than meta field?
- Display custom field on 404 page outside loop
- Why does this update_post_meta function not delete the custom field itself?
- 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
- WordPress loop by meta key that is an array? and how loop multiple arrays
- WP All Import / Update stock quantity from multiple XML files
- Problem with adding custom CSS class to image in ACF Photo Gallery plugin [closed]
- Concatenate Custom Field Value & HTML Value
- Output custom text field as unordered list
- `update_post_meta` not working anymore
- Order a WP_Query by meta value where the value is an array
- Unable to render custom field after attempt to generate a list of recent post in page template
- Add a custom class to the body tag using custom fields
- Hide a div when a custom field is empty
- Conditional On custom field plugin metabox
- Filter unique custom field value based on custom taxonomy category or other custom field
- Add “alt” Attribute to GD Star Rating Tags
- Display background color or image with custom meta box?
- custom header text
- Meta boxes only displayed when editing normal (default wp post_type) posts
- WordPress stripping out custom field tags
- Can ‘Custom Field’ data be inserted into this Short Code?
- Using an “IF” statement based on the existence of custom field
- Query pulling a single post per month
- Add custom fields from different posts
- Woocommerce Custom Meta Boxes- How to only display if they actually have content
- Updating Metadata with Shortcode
- Adding a custom meta field with default NULL value which is not selectable
- How do you save the values from custom fields to a products data
- Order category posts by last word of custom field
- Products listing check if meta checkbox is checked
- Is it possible to update the dataset using update_post_meta
- How to update custom fields when post is published?
- Hide a div if the fiels is empty
- Setting up a cron job to auto update a custom field
- Inserting a class on before_widget if checkbox is checked
- What is the correct way to search 3 custom fields only in WordPress?
- wp_get_nav_menu_items() with ACF
- How do I get the value of a current user’s custom field?
- Trying to retrieve meta values from three different meta keys and display in rows
- WP post meta – for loop inside for loop
- Store custom field’s multiple values in one user meta key
- Get field in readable word
- WooCommerce multiple custom fields code
- How to display custom field from a gallery thumbnail?
- WP_query meta_query slow with OR and DATES
- Get meta_query value by user meta array
- Getting a value from a custom field from a page that shares a parent with the current page
- meta field for numerous links
- Custom fields changes the formatting of metabox input
- Include a custom field in mysql query
- Display Content Based on Custom Field Value
- 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
- How can I put a custom field as the link of a button shortcode?
- Where can I find the code for the menu page meta boxes?
- I want Page titles and excerpts to show up on home.php in certain order
- Posts sortable column not sorting properly for custom field numbers
- 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]
- Appending an ACF custom field to the page title
- Displaying custom meta box value in a custom post page
- How to initialise WP_Query on the basis of a specific meta_value and continue iterating rest?
- I want to show image from custom field image on my custom page template
- Search custom post type posts only by meta fields?
- How to pass value to add_filter wpcf7_form_tag from another function?
- Custom Admin Menu Report for Specific User ID
- ACF number less than comparison not working