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

Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)

What else can I do to the query to do as little work as possible to eliminate “extra” posts in PHP that get queried because my query is too broad.

The trouble is, meta queries are slow. Metadata is a simple key => value type database. Keys are indexed, and values are longtext. That’s it. No datatypes. No additional indexes. Nothing. And not to mention each “rule” will cost you a
JOIN.

They’re fine for simple filters/switches/comparisons, but the more complicated you get, the more likely it is you should be using something else.

For the month view, I’m only displaying the post categories, the event_date, the title, and the total amount of posts per day. Is it possible to only query for this data to speed up the query?

If you can change the stored date format to MySQL’s default (YYYY-MM-DD), then you could use the casting feature of meta queries for date comparisons:

array(
    'key'     => 'event_date',
    'type'    => 'DATE',
    'compare' => 'BETWEEN',
    'value'   =>  array(
        date( 'Y-m-01', $time = current_time( 'timestamp' ) ),
        date( 'Y-m-t', $time ), // Last day of current month
    ),
),

This might give a boost, but no guarantee.

If all else fails, what’s a better way to handle this type of data in WordPress? I’d prefer to stay away from calendar plugins and use “Posts” in some way.

Without a doubt, another table. Primary index would be the post ID, and then the subsequent columns to store your arbitrary event data. I’m not a wiz at db schema, you’d be better off asking for advice specific to your problem on another StackExchange.

Hook onto save_post for updating the table, and use the awesome power of posts_clauses found in WP_Query::get_posts() to implement your joins and custom querying:

function wpse_175152_posts_clauses( $clauses, $wp_query ) {
    $clauses[ 'where' ];
    $clauses[ 'groupby' ];
    $clauses[ 'join' ];
    $clauses[ 'orderby' ];
    $clauses[ 'distinct' ];
    $clauses[ 'fields' ];
    $clauses[ 'limits' ];

    return $clauses;
}

add_filter( 'posts_clauses', 'wpse_175152_posts_clauses', 10, 2 );

Related Posts:

  1. Is it possible to retrieve all posts with a certain value for metadata?
  2. Efficient way of querying for a “fallback” post?
  3. How to update single value in multi dimensional Post Meta?
  4. Modern Tribe Calendar wp-query with meta query not working at all
  5. How to display custom field on homepage
  6. Can an array be used as a meta_query value?
  7. Use WP_Query in shortcode
  8. Get Current User Id Inside a Loop Returns 0 For a Shortcode
  9. Random order of WP_Query results with highest meta value
  10. WP Query with custom Shortcode
  11. Custom query, checking values of multiple meta keys
  12. Archive post by meta value + 24hours
  13. WP_Query: getting posts where custom field exists
  14. How to hide posts of a specific custom category in WordPress?
  15. Passing an array into WP_Query as a variable
  16. WordPress meta_query >= &
  17. Order a WP_Query by meta value where the value is an array
  18. Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
  19. Is it possible to order posts using multiple meta queries, i.e. show posts from first meta query, then the second?
  20. wp_Query with mutuplea values returns all posts
  21. meta_query search names when they have middle initials
  22. Usermeta data unserialize, extract and display in table in WordPress
  23. Delete post meta by serialized meta value
  24. Meta query not showing result properly
  25. How do I use WP_query with multiple post IDs?
  26. Inject post (from specific category) between posts in Loop
  27. Get meta_id along with meta_key and meta_value
  28. is_page() not working from within a plugin
  29. How can merge two arrays values in one array and save in database
  30. How to use multiple query with same meta key
  31. What is an efficient way to query based on post_meta?
  32. Passing JSON data from WP Query into AJAX causing NULL errors
  33. Can’t sort order of wp_query with 2 meta keys
  34. Compare two meta key values against each other inside the get_posts array?
  35. How to add 2 posts under another post? Formatting should be intact
  36. what is diference wp_get_attachment_url / wp_get_attachment_src / get_post_thumbnail_id?
  37. Order posts by meta value and Date
  38. Use ajax to update_post_meta
  39. Count number of published posts by type
  40. Can’t increase posts_per_page by variable
  41. List of posts by day of the week
  42. Get taxonomy terms only of the WP_Query current posts
  43. Change description on specific WooCommerce product status change
  44. How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?
  45. get current custom post ID by WP_Query method
  46. How to securely provide a $_POST var in WP_Query with PHP 7?
  47. Trying to remove post thumbnail with plugin
  48. Create WP_Query to search for posts by their categories or their parent/child categories
  49. WP_Query Check if post has one or more attached medias
  50. Dynamic content based on a URL parameter
  51. Get users that likes the post
  52. Get post id within comments loop
  53. How to get all author posts outside of author templates
  54. Grouping posts by a custom meta value
  55. How to get post ID in a Page?
  56. How to overwrite orderby with add_query_var
  57. How to override url params with rewrite rules vars?
  58. How to display SQL query that ran in WC_Order_Query?
  59. Store metakey value as an array
  60. Remove empty terms from array, sort alphabetically, update back to repeating field
  61. WordPress yoast seo plugin – parse snippet var in meta description
  62. Post not populating for custom post type based on category selection
  63. Using rules in Posts
  64. Infinite Loop – WP_Query
  65. Save the value of a wp_dropdown_pages
  66. WordPress SQL JOIN query
  67. WP_Query and help with the loop for magazine front page
  68. How to WP_Query posts order by parent title?
  69. search.php to search only the post title
  70. WP_Query – How to query all of post types categories
  71. How to work Woocommerce pagination inside shortcode?
  72. get value from get_post_meta then reuse it in another get_post_meta
  73. Meta query ignores multiple values of the key
  74. Use WPQuery to match to specific repeater row in post
  75. WP_QUERY post_in problem
  76. how to get data from two different table from wordpress database
  77. Using zip code to display custom data in Admin Order Details
  78. Assign meta_query value to php variable
  79. Query against multiple locations within single custom post type post
  80. WordPress search query, how to modify the sql
  81. Modify post image in full size
  82. Making list of posts with chosen description
  83. Ordering / grouping posts by datepicker ACF
  84. Include search tags and users in my search results system
  85. How to use something like meta_query but for fields?
  86. Add custom taxonomy to custom search for posts
  87. My query keeps looping infinitely ! how to stop it?
  88. How to add thumbnails from recent posts to owl-carousel in wordpress automatically?
  89. Only Get A Certain Number of Posts From WP_Query
  90. Querying posts based off a jquery datepicker
  91. Use custom get results query to show posts WordPress
  92. Values inside a custom field to determine which category posts to display
  93. Trying to combine multiple WordPress queries
  94. WP Query conflict on tag.php and category.php template
  95. WordPress post pagination on custom template not working
  96. Ajax Load More or View More functionality for woocommerce category layout by template overriding
  97. WP query with variables gives no result for specific user
  98. Display all categories (with link) of custom post type – WordPress
  99. Add adjacent post function inside custom recent post function
  100. How do I exclude the current post from the upcoming post query
Categories PHP Tags meta-query, php, post-meta, wp-query
Comparing a field with several values at once with meta_query
Extract image url associated to a category

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