Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Hide individual page title using checkbox in custom meta box?

$value is always empty because post ID is missing. Pass the parameter ($post object) to the hide_title_callback() function and get the post ID right way.

Also, you should take care of nonces.

<?php
// pass the $post object
function hide_title_callback( $post ) {

    // you have to verify this nonce inside `mysite_save_postdata()`
    // https://codex.wordpress.org/Function_Reference/wp_nonce_field
    wp_nonce_field( 'mysite_action_name', 'mysite_nonce_field_name' );

    // now you have the post ID
    $value = get_post_meta( $post->ID, '_mysite_meta_hide_title', true );

    if( $value == "on" ) : ?>
        <label for="hide">Hide: </label><input type="checkbox" name="hide" checked>
    <?php else : ?>
        <label for="hide">Hide: </label><input type="checkbox" name="hide">
    <?php endif;
}

Verify the nonce

<?php
function mysite_save_postdata( $post_id ) {
    // https://codex.wordpress.org/Function_Reference/wp_verify_nonce
    if ( !wp_verify_nonce( $_POST['mysite_nonce_field_name'], 'mysite_action_name' ) ) {
        return;
    }

    if ( isset( $_POST['hide']) ) {
        update_post_meta(
            $post_id,
            '_mysite_meta_hide_title',
            sanitize_text_field( $_POST['hide'] )
        );
    } else {
        update_post_meta(
            $post_id,
            '_mysite_meta_hide_title',
            sanitize_text_field( "off" )
        );
    }
}

To keep the database a bit cleaner I personally should not save the ‘off’ meta value. I should remove the _mysite_meta_hide_title completely if checkbox is unchecked. So, it will exist if ‘on’ and do not if ‘off’. if() statement in the hide_title_callback() will remain intact.

<?php
if ( ! isset( $_POST['hide'] ) ) {
    // unchecked
    delete_post_meta(
        $post_id,
        '_mysite_meta_hide_title',
    );
} else {
    // checked
    update_post_meta(
        $post_id,
        '_mysite_meta_hide_title',
        'on' // nothing to sanitize here
    );
}

Related Posts:

  1. post formats – how to switch meta boxes when changing format?
  2. Adding an assisting editor box to Post page
  3. Metabox Data not being saved [closed]
  4. Options to get my custom post type metadata via the WordPress API
  5. Show Custom Post Type meta boxes only on Page Edit
  6. Editing does not change post_name
  7. Add box with custom per-page properties
  8. Hide Page Title with Post Meta
  9. add post meta front end edit
  10. How to save HTML data into SQL post_content column
  11. I can not display meta value in extras.php and template-tags.php
  12. Add post meta data date to event
  13. Check if any meta on the post has value then display content
  14. Get or set values in post meta
  15. I created a Custom Meta Box but it is not displaying the value on my post page
  16. update_post_meta() throws Uncaught error: Cannot create duplicate attribute
  17. post_title in save_post action
  18. Auto save title as custom meta field value
  19. Remove action of an external plugin after checking if custom post meta is set
  20. How we insert values into database using metabox WordPress?
  21. Post meta box data not saving
  22. Add “upload media” button in meta box field
  23. Execute action after post is saved with all related post_meta records (data)
  24. How get post id from meta value
  25. Safe to delete blank postmeta?
  26. get_post_meta returning empty string when data shows in the database
  27. publish_post action hook doesn’t give post_meta_data
  28. How to access the post meta of a post that has just been published?
  29. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  30. Query between two meta values?
  31. List posts under meta_value heading
  32. get_post_meta doesn’t work
  33. loop through all meta keys with get_post_meta
  34. How to add multiple featured image in meta box in post editor?
  35. Change the post date from a meta box
  36. Saving html into postmeta without stripping tags – safe?
  37. How to show Published date and/or Modified date
  38. update_post_meta and get_post_meta not working
  39. wp_update_user isn’t instantly?
  40. Repeatable custom meta fields
  41. How to use update_post_meta inside wp_trash_post
  42. “update_post_meta” not working in “wp_insert_post_data” hook
  43. Display All Custom Post Fields and Values, Unless Empty
  44. How to create a meta_query to get all posts with a specific meta data?
  45. Why is my Custom Meta Box Field Inputs NOT saving?
  46. hide posts with specific meta data from admin page
  47. Post MetaTable Overload
  48. Custom RSS Feeds & Post Meta Data
  49. New WP_Query loop in admin causes problems
  50. Get a row from a separate table by matching a posts meta_key to a tables ID column
  51. get_post_meta is showing file url in url bar
  52. How to check if a post meta key/value pair already exists for a specific post
  53. Reset/delete post views meta/custom field after X hours or minutes or seconds
  54. Date format – Meta Box plugin
  55. How to Echo Metadata Value in Currency Format
  56. How to use media upload on metabox post page without breaking TinyMCE?
  57. Query posts WITHOUT a custom meta field
  58. Let’s Create Custom Field Template Documentation
  59. get_post_meta not working when variable used for post ID
  60. Update post meta dynamically
  61. Is it safe to add a new field to meta_value field?
  62. Need a SQL query to update meta_key=’_price’ with value in meta_key=’_regular_price’
  63. Display current user’s custom post meta in sidebar
  64. add_post_meta doesn’t work
  65. Can I save post meta programatically without setting metaboxes?
  66. Meta box data is saved but NOT displayed in the meta box text field. Why?
  67. How to get single value from get_post_meta() array of values?
  68. Editor meta box Showing but not saving
  69. Meta box values are displayed on Custom Fields list. Is it possible to hide them?
  70. wordpress update multiple posts post meta
  71. get_template_part() isn’t loading author information
  72. get Insert id for meta field
  73. Is there a way combine posts meta_name?
  74. Custom Meta Data is not being saved
  75. Format meta_value [closed]
  76. Removing Malware
  77. After updating the custom post type, metafields disappear from the post.php edit menu, how do I fix it?
  78. URL from get_post_meta() is broken my URL
  79. get_post_meta and add_post_meta not working
  80. Update Post meta with custom variable
  81. Export media library with metadata and import into new blog
  82. Multiple meta key and value search in the query
  83. Create Meta boxes dynamically
  84. How to show all the associated posts with specific date of data metabox?
  85. Retrieve posts from meta key
  86. Assign postmeta in bulk
  87. Problem with ‘save_post’ hook not running
  88. Howto: use existing post_meta as options for a different metabox (checkboxes or list)
  89. Re-order posts inside tax query
  90. wp_postmeta are updated for only one page
  91. Display articles related to a custom field on a page
  92. Related posts and custom meta_box?
  93. Using mysql queries to delete custom post types based on meta_value
  94. Write query according to post_meta
  95. Why values dont shows in custom post column?
  96. get_post_meta of multiple posts?
  97. Calling Data from Custom Meta Box
  98. I am stuck between post meta function to call unique id
  99. Cannot read properties of undefined (reading ‘useEntityProp’)
  100. Search posts by meta key in admin and front
Categories post-meta Tags metabox, page-template, post-meta, title
How to add toggle-able DOM element after nav menu item?
How can I cause run wp-cron to trigger sequentially?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress