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

Displaying custom posts only if custom meta box’s date is not expired

In your loop while(have_posts() ) : the_post(); you are unconditionally going through all posts, but since the loop stops after the_post() nothing actually happens. So that’s where the condition has to go:

while(have_posts() ) {
    the_post();
    if ($daysleft >= 0) {
         ... other actions
         }
    }

Your code looks a bit incomplete to me, so I’m not sure if my exact code will work, but this definitely is where you want the condition.

Please note that if the query has returned, for instance, 15 posts you may have less displayed if some of them are expired. If you want a fixed amount displayed, you must not change the loop which displays the result of the query, but the query itself.

Related Posts:

  1. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  2. How do I remove all the metaboxes for a custom post type?
  3. Get all posts from custom post type and insert in select input as metabox
  4. Detect meta value changes when post is updated (post_updated)
  5. Permalinks using event date (year & month) instead of publication date
  6. Grossly inefficient wordpress loops!
  7. How to sort CPT by custom meta value (date), and return posts month by month
  8. Having trouble with custom post type / meta box
  9. How can I include meta box content when searching?
  10. update a post meta from a single table cell TablePress
  11. update custom post type meta from a shortcode
  12. Custom Meta Boxes – Nonce Issue – Move to trash issue
  13. Displaying Meta Box Image
  14. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  15. How to add multiple featured image in meta box in post editor?
  16. Show metabox in custom-post-template depending on taxonomy term?
  17. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  18. Display custom post on home page based on a metabox selection
  19. Displaying Metabox value (custom post type taxonomy)
  20. How can I display my custom metaboxes on a custom post template?
  21. Meta Box Only in CPT Edit Page, Not in Add New
  22. get_post_meta is returning image id
  23. get_template_part() – post-meta not working?
  24. Create a random unique 6 digit number as custom field for custom post type
  25. Populate Custom Fields in a Custom Post Type?
  26. Custom meta box data array: foreach not working correctly?
  27. Undefined Variable – Custom Post Type Meta
  28. Populate dropdown from one custom post type inside another custom post type
  29. Meta Key Value in current-user-only loop
  30. How to check if user meta field is empty in conditional else statement
  31. A better way to add a meta box to custom post types
  32. Metabox not show in categories custom post type cmb2
  33. Show Custom Post Type meta boxes only on Page Edit
  34. Can’t publish custom post type – “You are not allowed to edit this post.”
  35. Meta Data for Custom Post Type not saving
  36. create custom meta box with default value
  37. Displaying a div from an assigned meta_value when on a page
  38. Create Array from custom post type to display a slider
  39. How to Echo Metadata Value in Currency Format
  40. Issue on Working with Metabox – Checkbox to enable Sale Items
  41. Why do I have to press the “Update” button twice to save my meta box values?
  42. Values from meta-box not on $_POST
  43. Custom post type suddenly stoped working after WordPress 5 update
  44. How can I output WPAlchemy repeating fields meta values in my page template?
  45. Meta box data is saved but NOT displayed in the meta box text field. Why?
  46. Save Metabox Custom Field Value
  47. Meta box data not saving
  48. WP_Meta_Query object with conditionals
  49. WordPress giving a 404 page when passing a year argument different than the current year
  50. Related posts meta_query CPT
  51. Meta box with front-end styling
  52. Sanitaizing Select Optin For Custom Post Type Metabox in WP
  53. Cannot Save MetaBox Data in Custom Post Type
  54. Add a class to post_class if more than one post shares same meta_value_num
  55. Function to return values from metabox
  56. Add a meta field to the list of results for a custom post type
  57. Empty meta-box returns publishdate if no value is set?
  58. Custom Meta Box with variable number of fields
  59. How to sort WP_Query by a custom text field (written as a date dd/mm/yyyy)
  60. Custom Post Type meta data getting deleted on bulk editing taxonomies
  61. How to avoid duplicate posts queried from custom meta?
  62. Custom meta fields not showing up in WP_Response Object via custom endpoint
  63. Get meta values from parent post and save in child post
  64. Metaboxes in Loop
  65. Custom meta box data not saving
  66. Collect custom post in a calendar
  67. get_post_meta for Custom Post Type ( CPT )
  68. Delete custom post type metadata without deleting the post in admin area
  69. How to check if meta box value is false for all posts then do something based on that
  70. Loop posts in a table ordered by a custom field value
  71. Problem with ‘save_post’ hook not running
  72. How to get past and upcoming post by defining date in custom field?
  73. Edit post meta direct from post.php?
  74. Howto: use existing post_meta as options for a different metabox (checkboxes or list)
  75. How to create content automatically when a post is published?
  76. Display a post count from a custom metabox selection
  77. loop through custom post-type with two meta_keys
  78. Post AND page parameter for WP function
  79. why get_post_meta is returning 0?
  80. Show search for data extracted from metabox
  81. Use a Variable in update_post_meta as the $meta_key
  82. Should I use a custom taxonomy or custom post type for grouping a list of panels associated with a group of tests?
  83. How to create review point system for CPTs (many-to-many relationship)
  84. Create post meta box that links another post
  85. Update query based on CPT / Post Type, show all sorted by date
  86. Saving a Custom Post Type with a Meta Box results in a “Updating Failed” error
  87. Loop all post on single.php
  88. After inserting new post with wp_insert_post() the post is not visble to WP_Query, but the same WP_Query works for post inserted from wp-admin panel
  89. Using publish_{custom-post-type} hook for custom post type to update meta doesn’t work
  90. Get_post_custom not fetching value from array wordpress
  91. Add a form in every post and save data in post meta
  92. Exclude a specific post in a Custom Post Type
  93. How can I write a function that would update any missing specific post metadata?
  94. Adding accordion-section to custom post type edit screen
  95. I’m having trouble with Related Product “tax_query” ‘terms’ value
  96. Custom post type paging return home page
  97. Pin posts to top of custom loop
  98. SELECT custom post type and its meta in SQL
  99. How to set post date in post_type so that it’s same as latest post in category
  100. How to use multiple Meta Field from CPT as Post permalink
Categories custom-post-types Tags custom-post-types, date, loop, metabox, post-meta
How do I change number of columns in a proper way in twentyfourteen child theme?
add_filter priority problem

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