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

get posts based on category and post meta

Possible Problems:

  • A change in the time zone could between 'End_Time' and date( 'G:i' ).
  • A change in the time between calling date( 'G:i' ) and date( 'w' ).
  • Repeated use of the date( 'w' ) function.
  • A meta query logic problem.

Any of these could be the culprit.

The time zone problem

If the time set in the 'End_Time' custom value is a different time zone from the web server, you might need to change date( 'G:i' ) to adjust that time zone change. I do not know all the ramifications of using this call:

date_default_timezone_set('Europe/London');

You should probably do some testing near midnight (server time) to see what date( 'G:i' )and date( 'w' ) are returning and compare them with typical 'End_Time' custom values from posts.


The date( 'w' ) function

You check it repeatedly, but what happens near midnight on the server?

     if ( date( 'w' ) == 0 ) {
else if ( date( 'w' ) == 1 ) {
else if ( date( 'w' ) == 2 ) {
else if ( date( 'w' ) == 3 ) {
else if ( date( 'w' ) == 4 ) {
else if ( date( 'w' ) == 5 ) {
else if ( date( 'w' ) == 6 ) {

Near midnight the value in date( ‘w’ ) could change. If it does, there is a chance that $args is either unset or set to a value with the wrong category.

It would be better to check date( 'w' ) only one time:

$day_of_the_week = date( 'w' );

     if ( $day_of_the_week == 0 ) {
else if ( $day_of_the_week == 1 ) {
else if ( $day_of_the_week == 2 ) {
else if ( $day_of_the_week == 3 ) {
else if ( $day_of_the_week == 4 ) {
else if ( $day_of_the_week == 5 ) {
else if ( $day_of_the_week == 6 ) {

You really do not need all the if else if stuff. Since you are only changing the 'cat' argument, this should do the same thing without all that typing.

$time = date( 'G:i' );
//                                    S   M   T   W   T   F   S
$day_of_the_week_categories = array( 17, 18, 12, 13, 14, 15, 16 );
$day_of_the_week = date( 'w' );

// The Query
$the_query = new WP_Query( array(
    'cat'            => $day_of_the_week_categories[ $day_of_the_week ],
    'posts_per_page' => 1,
    'offset'         => 0,
    'orderby'        => 'meta_value_num',
    'order'          => 'ASC',
    'meta_query'     => array(
        array(
            'key'     => 'End_Time',
            'value'   => $time,
            'compare' => '>',
            'type'    => 'TIME',
        ),
    ),
);

Change in the time between calling date( 'G:i' ) and date( 'w' )

Close to midnight (server time) the call to date( 'G:i' ) might occur on Tuesday and the call to date( 'w' ) might occur on Wednesday. This might be fixed by changing the code above to this:

$now  = time();
$time = date( 'G:i', $now );
$day_of_the_week = date( 'w', $now );

The PHP date() function gets the time from the PHP time() function. By doing all time calculations off a single call to time(), $time and $day_of_the_week should always refer to the same time and day.


A meta query logic problem.

This problem is harder to diagnose without working directly on the web site and testing the posts there. It is very important that all date and time functions used to calculate 'End_Time' are also used to calculate $time and $day_of_the_week. Similar midnight problems may arise when calculating 'End_Time'.

Related Posts:

  1. Display post details by post ID
  2. Display post’s description and caption
  3. How do I get posts by multiple post ID’s?
  4. What does setup_postdata ($post ) do?
  5. Query for custom post type? [closed]
  6. setup_postdata() does not seem to be working?
  7. Regenerate Slugs From Title of Posts
  8. get_posts – get all posts by author id
  9. Why get_posts are only showing five posts (retrieved by assigning a category to them?
  10. Exclude Current Post from Recent Posts Loop
  11. WordPress get_posts by category
  12. Order by meta value or date?
  13. get_post random and order by not working
  14. Alternative to get_posts() due to multithreading cache crash
  15. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  16. How to get current post ID in quick edit callback
  17. get_posts with meta_compare=’LIKE’ not working
  18. How to restore deleted pages/posts?
  19. Sort “get_pages” by menu order not ordering
  20. get posts based on meta value of the author
  21. date_query not returning some posts in date range
  22. Get the exact SQL query that get_posts() generated
  23. Ordering posts having multiple post-meta date fields
  24. How do you get posts by meta_query using the JSON API plugin?
  25. Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?
  26. Query posts ordering by title, but ignore ” and special characters
  27. Why time functions show invalid time zone when using ‘c’ time format?
  28. Whats the way to format a date after using get_posts()
  29. get_posts inside cron
  30. get_posts not honoring post_status
  31. Query between two meta values?
  32. get_posts returning empty array
  33. get_posts only children from certain parents
  34. Problem with get_posts, tax_query and counting the number of posts
  35. get_posts cannot grab from specific category
  36. WP Query post meta value
  37. Filtering custom post type query
  38. Check If posts exist in custom post type category, outside of loop
  39. Get posts by meta value with date
  40. data returned from get_post($postId) have different keys than wp-json/wp/v2/posts/{postId}
  41. Order posts according to user defined order for meta values?
  42. How to limit get_posts()?
  43. meta_query date and time comparisons
  44. get_posts from post x(offset=>x) to end
  45. With get_posts(), how can I put a category as a variable
  46. What’s the difference between “get_posts” and “wp_get_recent_posts” when used with “setup_postdata”?
  47. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  48. Serialized array, grab specific posts with meta_key/meta_value[0]->is_featured
  49. How do I combine these 2 queries
  50. Using date stored as custom field to filter posts displayed in admin
  51. get_posts gives different result than wpdb->get_results
  52. Increase the page size of the WordPress REST API
  53. How to generate a list of posts published on current day?
  54. Trouble using get_post
  55. Ordering posts by metadata
  56. Cache Get_posts
  57. Get latest posts from multiple categories
  58. get_posts() returns all posts rather than the ones specified with ‘post_author’ =>
  59. Query random post from different categories
  60. Get_Posts, only if in both categories
  61. Remove posts from query for events whose start date has passed
  62. Posts not showing in correct Alphabetical or ID order when using get_posts / orderby but only on production server
  63. get_posts() excluding all children of a specific post/page
  64. Secondary Query Is Breaking Main Query
  65. If clauses in get_posts query
  66. var_dump and print_r cause white screen
  67. `get_posts()` ignore my custom post
  68. How to show Published date and/or Modified date
  69. excluding current post from get_posts
  70. How to query posts to include specific pages of one post type, and all pages of another post type?
  71. Only retrieve posts where post_excerpt has been filled out
  72. Can I use numberposts=-1 and offset together when using get_posts()?
  73. Ordering posts in get_posts
  74. I don’t arrive to do order_by title when i have a conditionnal year in a request
  75. How can I display a single post link, based on title sample and change monthly?
  76. get_posts() doesn’t consider user permissions
  77. Meta query: get posts with value in a multidimensional array
  78. Get posts inside Get terms problem
  79. If meta key exists in get posts function otherwise create it
  80. Better way to list links with different meta values using same meta key?
  81. Ho to add get_relative_date and post_except to this get_latest_post query?
  82. How to update all posts but the current one (post__not_in not working?)
  83. Only showing the_date and the_excerpt for first entry in get_posts
  84. get_post_meta slowing down my page load (in a plugin)
  85. How to get the post content from a category on my homepage
  86. get_posts() not working in functions.php
  87. Query Problem in getting top viewed posts
  88. get_posts in meta box dropdown not showing latest posts
  89. get_posts not working as expected
  90. Is it possible to retrieve a post and its metadata at the same time?
  91. Get all pages and posts with get_pages() or get_posts()
  92. Getting only a specific post type with get_post?
  93. What is the p parameter in a get_posts() parameter array?
  94. Exclude post format from get_posts
  95. Memory issue with get_posts( ) function
  96. get_posts() using an array of post ids [closed]
  97. Get only used meta_values
  98. Time & Date on Post – Time Ago Custom Function
  99. Iterate over get_post_meta() result
  100. Recoverable Fatal Error – Object of class WP_Post could not be converted to string
Categories get-posts Tags date-time, get-posts, post-meta
Replace get_the_title and the_exerpt with custom arrays
Looking to implement a scrolling gallery in my theme

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