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 custom fields when empty

There are a few issues that break your code:

  • $post doesn’t exist, you’d need to import it using global, or simply use get_the_ID as you’re already doing
  • $check[''] won’t return anything since it will access an undefined index, outside of the empty construct it would trigger a warning

I can’t test it right now, but this should work, at least syntax wise (and according to the API documentation theoretically also functionality wise):

function headway_title_button()
{
    $post_id = get_the_ID();

    if(metadata_exists('post', $post_id, 'titel_knop'))
    {
        printf(
            '<div class="title-btn"><a href="https://wordpress.stackexchange.com/questions/67540/%s" alt="https://wordpress.stackexchange.com/questions/67540/%s"><img src="https://wordpress.stackexchange.com/questions/67540/%s" /></a></div>',
            get_post_meta($post_id, 'titel_knop_url', true)
            get_the_title(),
            get_post_meta($post_id, 'titel_knop', true)
        );  
    }
}

However it might be more efficient to fetch all metadata at once. But WordPress isn’t smart enough to unserialize it when not requesting a single value, so you would need to do that on your own (if neccessary at all, depends on the metadata):

function headway_title_button()
{
    $meta = get_post_meta(get_the_ID());

    if(!empty($meta['titel_knop'][0]))
    {
        printf(
            '<div class="title-btn"><a href="https://wordpress.stackexchange.com/questions/67540/%s" alt="https://wordpress.stackexchange.com/questions/67540/%s"><img src="https://wordpress.stackexchange.com/questions/67540/%s" /></a></div>',
            maybe_unserialize($meta['titel_knop_url'][0]),
            get_the_title(),
            maybe_unserialize($meta['titel_knop'][0])
        );  
    }
}

Related Posts:

  1. post meta data clearing on autosave
  2. Can I exclude a post by meta key using pre_get_posts function?
  3. Custom post meta field effect on the performance on the post
  4. How to get custom post meta using REST API
  5. Difference between meta keys with _ and without _ [duplicate]
  6. Orderby meta_value only returns posts that have existing meta_key
  7. What is the index [0] for on post meta fields?
  8. What is “meta_input” parameter in wp_insert_post() used for?
  9. How to enable revisions for post meta data?
  10. The “_encloseme” Meta-Key Conundrum
  11. Best way to programmatically remove a category/term from a post
  12. Using get_post_meta with new_to_publish
  13. Custom field metabox not showing in back-end
  14. So much data in postmeta
  15. Can I count the number of users matching a value in a multiple value key?
  16. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  17. How to hide meta box values from custom fields list?
  18. Auto sort the wp-admin post list by a meta key
  19. WP doesn’t show Array Custom Fields?
  20. get_post_meta() unserialize issue – returns boolean(false)
  21. What is the advantage of the wp_options design pattern?
  22. Storing meta fields multiple times OR once with multi dimensional array?
  23. Allow user to create instances of custom field
  24. display specific custom fields
  25. Add new “Insert Into Post” button with another function
  26. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  27. Meta keywords and descriptions plugin for manually editing meta for each page/post
  28. Add options to featured image
  29. passing argument to get_template_part() or a better way to code
  30. Add meta data to the menu
  31. Is it possible to store arrays in a custom field?
  32. Get updated meta data after save_post hook
  33. Detect meta value changes when post is updated (post_updated)
  34. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  35. Save HTML formatted data to post meta using add_post_meta()
  36. Search everything (posts, pages, tags, cpt, meta)
  37. importing data from non-wordpress mysql db
  38. can you set a default value to a custom field
  39. Create meta boxes that don’t show in custom fields
  40. Transients vs CRON +Custom Fields: Caching Data Per Post
  41. Unable to save datetime custom meta field using update_post_meta() function
  42. Up/Down voting system for WordPress
  43. Guest Author – How can I use custom fields to create guest author link?
  44. Create custom field on post draft or publish?
  45. Display info from custom fields in all images’ HTML
  46. Ordering posts by anniversary using only day and month
  47. get_post_meta fields don’t show up on posts page
  48. Update meta values with AJAX
  49. How to break meta values into different items and avoid duplicates?
  50. copy attachments to another post type and change attachment url
  51. Cannot edit post meta fields with rest API
  52. ajax delete value from custom field array
  53. Save attachment custom fields on front end
  54. How can update custom meta for all posts
  55. Assign category using custom field?
  56. How to use pagination with get_post_meta
  57. Copying Custom Meta Values from existing post to a duplicate post
  58. Add input field to ‘Pages > Edit Page’ through functions.php
  59. Add a post meta key and value only if it does not exist on the post
  60. Move value of one custom field to another
  61. Order posts according to user defined order for meta values?
  62. Displaying posts with only upcoming dates according their custom field date value
  63. Auto-add paragraphs to custom field?
  64. Add description text under input field for new profile fields
  65. Custom fields to save multiple values
  66. Custom fields: In what order are they saved into the DB?
  67. Function to change meta value in database for each post
  68. How to add a predefined custom field without using a plugin?
  69. Get a post_id where meta_value equals something in a serialized meta_value field
  70. Get aggregate list of all custom fields for entire blog
  71. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  72. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  73. Custom field value based on other custom field values
  74. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  75. Populate Custom Field Dropdown on Theme Install?
  76. How to save a ToggleControl value in a meta field?
  77. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  78. Saving custom image meta fields
  79. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  80. How to display Meta Field Value?
  81. MySQL query to set wp_postmeta using term_taxonomy_id value
  82. How to Validate Post Meta type/extension (Video File Image File etc)
  83. Get all meta keys assigned to a post type
  84. How to add content at the end of posts?
  85. using multiple meta_key and meta_value in query_posts
  86. Custom Meta Box not Saving in Posts with Gutenberg Editor
  87. Adding custom fields (post meta) before/during wp_insert_post()
  88. Combine multiple custom field values into single value
  89. How can I sort homepage by a meta value?
  90. Get specific custom field keys from a post and put into an array
  91. How do I use wp_query for WordPress search?
  92. Nav Menu – Add class based on meta keys
  93. How to query posts with certain custom meta data, and output Post data
  94. MySQL Query that looks for post with Custom Field, then changes Category
  95. ACF: How to get the full field name (meta_key) by a field key?
  96. How to wrap meta values seperated by comma in ? [closed]
  97. Bulk remove post meta
  98. post meta getting deleted on save
  99. How to create html block to display extra information on woocommerce single product page
  100. Can’t get post ID using wp_insert_post_data
Categories custom-field Tags custom-field, functions, post-meta
wp_nav_menu check both hierarchy fail?
Correct attachment size

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