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. Display posts in correct month order using single date custom field
  26. Front end file upload returning wrong attachment url
  27. Toolbar Hidden in a Virtual Page
  28. Query posts by searching for a string in a meta field
  29. Post meta select input, if statement
  30. displaying a fall back query if there’s nothing in the post-type category
  31. WordPress SQL JOIN query
  32. Using multiple variables to assign categories to an array
  33. WP_Query meta compare must include ALL array values
  34. Search.php gets metadata from first post
  35. WordPress stripping out custom field tags
  36. the_post(); prints out style text into my HTML?
  37. WP_Query and help with the loop for magazine front page
  38. Modifying WP URL handing code?
  39. Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
  40. WP_Query: How to get results from both meta_key options?
  41. orderby meta_value_num is not working, giving default order
  42. Wp Query : Order by distance lat,lon
  43. How can I modify this code to make the search box include tags and meta
  44. Free search and custom taxonomy query not providing the same result
  45. How to WP_Query posts order by parent title?
  46. How do I reset $wp_query in a function?
  47. search.php to search only the post title
  48. Let current user know pending posts counts using wp_query
  49. How to check post meta isset or not empty during publish/save post?
  50. Output product category link from WP_Query
  51. WP_Query – How to query all of post types categories
  52. get_terms with specific id order
  53. How to work Woocommerce pagination inside shortcode?
  54. How to sort WooCommerce products page by latest in-stock items first?
  55. WP_Query for woocommerce products with a pattern as a post_title
  56. get value from get_post_meta then reuse it in another get_post_meta
  57. “pre_get_posts” orderby custom date field in different format?
  58. wp_query with multiple arguments with AND
  59. WordPress Search Ajax + Isotope + InfiniteScroll
  60. How do I make these combination select filters work when only one dropdown is set? They work in WordPress 5.8.2 but don’t in 5.8.3
  61. Passing in MySQL prepare statement parameter separately throwing error
  62. Order category posts by last word of custom field
  63. Replace a single variable with add_filter
  64. Nested Queries of decreasing specificity
  65. get different meta-data of a complicated query at the same time
  66. Wrong pagination results, page counter resets when navigating
  67. Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
  68. How do i create a custom post query when the meta value is an array?
  69. Sending simple variable on single page to WP JSON
  70. How to exclude meta no index pages from wp_list_pages
  71. Using a new WP_Query inside the loop
  72. wp query to use both author id and meta_query
  73. Load more posts using AJAX based on posts inside WP_Query
  74. Loop for recently (updated_post_meta) posts?
  75. How to add and subtract user meta values after post meta update
  76. in_array not working on dev server but works on localhost
  77. wp_query – Exclude the first thumbnail from lazy loading on archives
  78. How to call my PHP function with AJAX ? wp_query
  79. WordPress sorting posts by date and title using a dropdown
  80. Custom search query on WordPress page not working
  81. WP query posts BUT show the most recent and one per author
  82. How to update custom fields when post is published?
  83. Update Images after edit via php
  84. How to get specific multiple pages excerpts at homepage?
  85. Ajax filter with loadmore button
  86. Define global custom post
  87. How to select post ID for given parent name with nested prepared queries?
  88. Meta query ignores multiple values of the key
  89. posts_per_page displays only 2 posts instead of 4 posts
  90. Trouble with serialized metadata
  91. No more get_option(‘date_time’) in WordPress 5.5?
  92. Use WPQuery to match to specific repeater row in post
  93. Query by pagename not Working
  94. Query on a repeater date (acf)
  95. WP_QUERY post_in problem
  96. Display featured image of post type category
  97. how to get data from two different table from wordpress database
  98. show most viewed post
  99. Using zip code to display custom data in Admin Order Details
  100. can I extend the WP_Query class to deal with ‘duplicate’ posts created by joining to wp_posts?
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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