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

Can’t check if a post has thumbnail adding filter to get_post_metadata()

Option 1: force featured image on all posts

Use the save_post hook, which runs twice per “save” action if you have revisions enabled. You can verify it’s the final run by checking that there is $_POST data (it’s empty the first time around).

add_action('save_post', 'wpse_force_featured_image', 20, 2);
function wpse_force_featured_image($post_id, $post) {
    // If this is a "Post" and $_POST data is present
    if(count($_POST) > 0 && $post->post_type == 'post') {
        // Check if featured image is missing
        if(!isset($_POST['_thumbnail_id'])) {
            // Add your desired fallback featured image
            add_post_meta($post_id, '_thumbnail_id', 'my_fake_id');
        }
    }
}

You’ll have to make sure that the ID is a real image in the Media Library. Also, this will only work for new Posts, and Posts you update after the code is added. Any old Posts will have to be updated one by one.

Option 2: update theme to include fallback

Wherever you’re using the featured image in the theme, you could just add a fallback image. So for example, if in single-post.php you have something like

if(has_post_thumbnail()) {
    the_post_thumbnail();
}

you can change this to

if(has_post_thumbnail()) {
    the_post_thumbnail();
} else {
    echo '<img src="https://wordpress.stackexchange.com/questions/356627/image_url.jpg" alt="alt text" />';
}

The benefit is, this will add a fallback immediately for all Posts – you won’t have to go back and update them or add new ones to see this in action. (If you don’t already have a child theme or a custom theme, you’ll want to make one so you’re not editing parent theme files.)

Related Posts:

  1. Add options to featured image
  2. Best way to programatically add “rel” attributes to page and post images
  3. Can’t filter wp_get_attachment_link
  4. How to hide image-url if no attachment?
  5. What are the meta fields for an attachment?
  6. remove_filter function to unhook twentysixteen parent theme function
  7. Best way to add image to recent posts widget?
  8. Set thumbnail from URL, by grabbing image in functions.php
  9. Redirect to another page using contact form 7? [closed]
  10. append to existing parent theme function
  11. Get ID of first image attached to a post
  12. How to stop wordpress from injecting hard-coded style into image attachments with captions
  13. It does not work concatenation in function.php
  14. Add Element as a Filter to the_content
  15. Override the WordPress core function wp_referer_field
  16. Modify post filter to set custom number of posts per page and exclude child posts
  17. Make wp_link_pages() suitable for Twitter Bootstrap markup
  18. Exclude from the query posts with meta_key and meta_value
  19. How can I add a filter for specific categories on functions.php?
  20. How to add custom JavaScript in functions?
  21. Setting a default text for excerpts of a particular category
  22. How to edit the Tags within the image file URLs?
  23. How would I go about replacing this function in my child theme located in inc/template-tags.php
  24. Retrieve a value from Yoast SEO to use to set a default twitter card image honoring overrides
  25. Can’t properly set the_title add_filter to show short_URL
  26. Add_filter when value is no variable?
  27. How to retrieve the current post’s generated featured image size?
  28. Check if the image size is available and if not use ‘full’ image size
  29. How to add_filter html template to middle of content
  30. Prevent custom field from being updated after first publish
  31. (Woocommerce) Order by price when entering specific category
  32. Filter an WordPress Function in (general-template.php)
  33. Get array of metakey in all posts
  34. Redefine function arguments before rendering
  35. User function to return multiple get_post_meta()
  36. How to edit classes in body tag?
  37. is there a way to remove featured image from blog page and single page
  38. attachment page template? only show attachments for current post?
  39. Filter nav menu items HTML tags and wrap inner text with span
  40. How to get an attachment id from a filename
  41. Possible to hook into Media Library preview File column and use a custom image?
  42. How can I add a class to a nav li depending on URL?
  43. Can set_post_thumbnail be used to remove a thumbnail?
  44. Can’t locate custom image sizes defined by child theme
  45. Modify gform_other_choice_value for specific form and specific field in Gravity Forms
  46. Combine embed_oembed_html and oembed_result
  47. login_headertitle is deprecated since version 5.2.0
  48. Set “woocommerce_is_purchasable” to false for specific “$product->is_stock_status”
  49. How to display the_archive_title() and the_archive_description() – “weird” interaction
  50. Filter to wp_list_authors
  51. How to add custom li item to wordpress menu
  52. Native gallery custom html output
  53. The_content and Preg_replace in loaded Iframe [closed]
  54. How to edit/replace Parent functions.php function in Child Theme to add “Walker” class
  55. How to add a new image size and apply it to posts only?
  56. Functions Filter Question [closed]
  57. Is it possible to use add_filter in an included file in the child theme’s functions.php?
  58. Remove attachment page for audio media type only
  59. Insert Content Before div#main from the functions.php File
  60. Add Adsense code between job listings – wp job manager plugin
  61. Get Attachment Category Name
  62. Problem with images URL after filter applying
  63. remove_action not working, even after changing priority [duplicate]
  64. remove/hide wp-editor
  65. Replace header image on all other pages but home – URL issue
  66. Add a class to post if it has been recently updated
  67. search form leads to 404
  68. Show Featured Image in else statement
  69. Change custom featured image size in twentythirteen child theme
  70. how to add_filter to non hook function
  71. resizing of thumbnails not working
  72. Featured Image keeps cropping
  73. show all the posts thumbnails
  74. Changing the text of Upload/Insert on Posts and Pages Screen
  75. How d0 i get the number of attachments in the post
  76. struggling with syntax for the_post_thumbnail();
  77. include w_thumbnail_src in function?
  78. Any adverse effects of adding apply_filters to a function?
  79. Conditional custom menu?
  80. Get URLs for AJAX Filter Checkboxes WordPress
  81. How to avoid saving empty data to sql while using add_meta_box
  82. How to change text color depending on the number value (Using javascript)
  83. Custom image sizes showing in Classic Editor only when upladed directly to post
  84. how can I add filter in specfic field in my website?
  85. Different image using srcset function
  86. How to display an image before title text in menu items
  87. How to add custom metakey to shop_order page’s searching function?
  88. Adding custom social icons to the social media icon block in the Gutenberg editor?
  89. Auto delete content in specific folder inside media library
  90. Which filter fires upon setting a featured image
  91. Customizing the wp_video_shortcode output with add_filter
  92. Filter works on last selection but no others
  93. Overwrite text in a complicated filter hook
  94. WP_Query for Attachments not working as expected
  95. How to add HTML into error message
  96. How to update an image attachment’s alt text from a custom field when saving a post?
  97. Can I use ‘Featured Image’ as a hero image and a thumbnail?
  98. Save_post – Warning: Cannot modify header information
  99. Update wp_postmeta table based on 2 keys
  100. Custom attachment function not working in v5.4.2?
Categories functions Tags attachments, filters, functions, post-meta, post-thumbnails
Huge debug.log because of “undefined offset in media.php”
write custom woocommerce templates and forms

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