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

Need help on creating If-statement for custom meta fields

This gets asked a lot so lets try and fully explain it.

We can simply wrap it in an if statement and echo the value, for example,

   <?php if ( get_post_meta($post->ID, 'genre', true) ) : ?>
     <?php echo get_post_meta($post->ID, 'genre, true) ?> ?>
   <?php endif; ?>

But that is ugly, and why do 2 queries when you can do one instead? So we will put the post_meta value into a variable, like $film_genre = get_post_meta($post->ID, 'genre', true;.

This would look like:

$film_genre = get_post_meta($post->ID, 'genre', true);

  if ( $film_genre ) {
    echo 'something is here';
  }
  else {
    echo 'nothing is here';
  }

Furthermore I find the function a little wonky in terms of checking whether it is empty or not so I add an additional check just to make sure using !empty ( this checks to see if the meta box value is NOT empty).

That looks like:

 $film_genre = get_post_meta($post->ID, 'genre', true);

  if (!empty($film_genre)) {
    echo $film_genre;
  }

But that’s not it! Since your example uses 7 meta boxes, let just use one query function to grab them all using get_post_custom. http://codex.wordpress.org/Function_Reference/get_post_custom

That would look something like:

$film_meta = get_post_custom( $post->ID );
if ( $film_meta ) 
{ 
    echo $film_meta['genre'];
    echo $film_meta['rated'];
    echo $film_meta['releasedate'];
    echo $film_meta['runtime'];
    echo $film_meta['director'];
    echo $film_meta['cast'];
    echo $film_meta['grade'];
}

Now that is much better, it might look silly echo-ing tons of stuff in a row, but this is just an example, typically your adding some markup around the values or perhaps additional code, the important part is that your only using one function, and it is clean and easy to read/understand and output.

ps. Also note the the 3rd parameter of get_post_meta set to “true” does not mean the value is intuitively true, but rather sets the result to a single value, and returns nothing if empty.

Related Posts:

  1. wp_enqueue_script adding conditional statement not working
  2. Check if first paragraph is an image, then show custom code right after it?
  3. If post author role is X
  4. Can a conditional statement apply to part of a slug?
  5. Add default content to posts in a specific category?
  6. is_front_page, is_page(‘slug’), is_page(id) not working
  7. How to make gravatar.com avatars conditional?
  8. Loading Scripts on Specific Pages
  9. Condition function for is parent category?
  10. Conditional Statement – Best Way to Remove Nav on Contact Page
  11. Proper syntax for simple conditional bloginfo language
  12. wordpress is_page() problem
  13. Location-Based Content
  14. Personalized message for each unique password-protected page
  15. Conditional Shortcode image display
  16. Change Woocommerce order button page on particular page
  17. Change a url / link if a user is logged in?
  18. to create own conditional tags for business directory in wp
  19. How to use “Cases” instead of “IFs” for conditional logic
  20. Which is the better way to write a conditional statement? [closed]
  21. Nested conditionals
  22. Is there a way to password-protect part of a post?
  23. if/Else have_posts Else fails to echo message to page
  24. Conditonal statement for iPad
  25. Return function only on certain pages
  26. How to write this conditional statement?
  27. Conditional for Custom Post Types
  28. Date-Based Conditional Tag
  29. Message box when accessed from iPad
  30. Show this code if user has previously left a comment
  31. Insert a conditional in the middle of a function to give it 2 different outcomes
  32. How can I use ‘edit_form_after_title’ conditionally?
  33. Force Log in to view a page
  34. Conditional loading of CSS for my plugin
  35. Looks like this if condition is not working [closed]
  36. Generating images from an array of categories
  37. Conditionally run function based on custom meta value?
  38. WP conditional site logo and header block
  39. How to know if the most recent article
  40. Widget logic conditional widget
  41. How to display image on condition that a selection has been made
  42. How to Conditionally Not Display a Link Based on Current URL?
  43. Allow users to only CREATE one single (custom) post
  44. If statement to check for post_content
  45. why is my custom loop failing?
  46. Conditional sidebar menu
  47. What is the best way to disable my WP website if the user has adblocker on? [closed]
  48. How to tie two conditions to one statement
  49. Create a page from different content blocks
  50. Content Restriction but allow public REST API
  51. How do test if a post is a custom post type?
  52. How do I retrieve the slug of the current page?
  53. Enqueue Scripts / Styles when shortcode is present
  54. Query all posts where a meta key does not exist
  55. Most efficient way to get posts with postmeta
  56. How to only display posts whose meta_value field is not empty?
  57. Get posts by meta value
  58. Can the Next/Prev Post links be ordered by menu order or by a meta key?
  59. what is the correct way to compare dates in a WP query_posts meta_query
  60. How to hook update_post_meta and delete_post_meta?
  61. Explanation of update_post_(meta/term)_cache
  62. wp enqueue style on specific page templates
  63. How to extract data from a post meta serialized array?
  64. Can I exclude a post by meta key using pre_get_posts function?
  65. Post meta vs separate database tables
  66. Advanced search form with filters for custom taxonomies and custom fields
  67. How to update custom fields using the wp_insert_post() function?
  68. How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
  69. Can wp_query return posts meta in a single request?
  70. order by numeric value for meta value
  71. Attaching taxonomy data to post with wp_insert_post
  72. Passing error/warning messages from a meta box to “admin_notices”
  73. Add “upload media” button in meta box field
  74. How to save an array with one metakey in postmeta?
  75. WordPress is stripping escape backslashes from JSON strings in post_meta
  76. Display Yoast WordPress SEO title in archive template
  77. Loading scripts only if a particular shortcode or widget is present
  78. Why WordPress choose data serialization over json_encode?
  79. How to make search include data from wp_postmeta?
  80. Meta query with boolean true/false value
  81. How can I get the post ID from a WP_Query loop?
  82. Why isn’t is_page working when I put it in the functions.php file?
  83. Why are simple updates to wp_postmeta’s “_edit_lock” so slow?
  84. How to check if post meta key exists or not in wordpress database
  85. Custom post meta field effect on the performance on the post
  86. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  87. How to get custom post meta using REST API
  88. How do I query for posts by partial meta key?
  89. Difference between meta keys with _ and without _ [duplicate]
  90. Use REGEXP in WP_Query meta_query key
  91. How to conditionally enqueue a stylesheet only for a certain page(s)?
  92. Get post with multiple meta keys and value
  93. Correctly delete posts with meta and attachments [duplicate]
  94. Orderby meta_value only returns posts that have existing meta_key
  95. What is the index [0] for on post meta fields?
  96. Order by meta value or date?
  97. What is “meta_input” parameter in wp_insert_post() used for?
  98. How to hook a filter to catch get_post_meta when alternate a custom field output?
  99. How to update_post_meta value as array
  100. How to add category to: ‘wp-admin/post-new.php’?
Categories conditional-content Tags conditional-content, post-meta
Speed up WordPress
permalink for category pages and posts

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