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

Best way to check if a post with specific meta exists

You can choose between simplicity and efficiency.

Running a get_posts() with a meta query for every entry is the simplest approach, and will do the job. Of course, as you pointed out, there are performance considerations.

If you are looking for a more efficient solution, you will have to dive as deep as the database layer and run your own SELECT query. Then, store the results in an associative array and check every entry against that.

Something like the following:

global $wpdb; // if we're in a function
$q="
  SELECT $wpdb->posts.ID,$wpdb->postmeta.meta_value
  FROM $wpdb->posts 
  JOIN $wpdb->postmeta ON ($wpdb->posts.ID=$wpdb->postmeta.post_id)
  WHERE $wpdb->post.post_type="event" and $wpdb->postmeta.meta_name="xml_id"
  ";
$rows=$wpdb->get_results($q,OBJECT);
$events_idx=array();
foreach($rows as $row) $events_idx[$row->meta_value]=$row->ID;

After that, checking if an event already exists is as easy as isset($events_idx[$xml_id]). And if it eventually exists, you would update the post with id given by $post_id=$events_idx[$xml_id].

Related Posts:

  1. How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
  2. Get ID of a page containing secondary loop in content
  3. How to add “time” data this?
  4. Next and Previous loop
  5. Obtain page data and use regular loop
  6. How to hook into container
  7. Output meta into arrays
  8. value of metadata is null wrong use of if statement
  9. Sending mail not working correctly
  10. make get_post work in the loop
  11. using update_user_meta to rank users
  12. get_post_meta printing empty fields, but it shouldn’t be
  13. Check value of post meta within IF statement
  14. WP update_post_meta link loop
  15. Humanmade | Custom meta box class: How to display a repeatable meta box group?
  16. Display articles related to a custom field on a page
  17. Split Content and Gallery
  18. Redirect loop when trying to login to /wp-admin/ [duplicate]
  19. Do I need to use The Loop on pages?
  20. How can I custom order the results from wp_list_categories?
  21. Display all authors and their only one latest post
  22. Remove comma from last item output from loop
  23. as a shortcode within the loop?
  24. Page template not displaying
  25. Can I use both a custom excerpt and a trimmed excerpt?
  26. get_template_part() – post-meta not working?
  27. How to get_template_part using AJAX?
  28. How can I modify the loop from a plugin without wiping out other filters?
  29. I’ve been out of the loop since 2.8. What did I miss?
  30. Loop first six posts in carousel, next eight in grid
  31. Converting a dynamic piece of code using WordPress Loop into a static one using Post ID
  32. Infinite blog loop
  33. One custom loop with condition to check child posts
  34. Loop get_theme_mod
  35. Move sticky posts down in main loop
  36. Why only one post (and no pagination) on this variation of the loop?
  37. Loop in Related Posts WordPress
  38. Conditional query that displays on Woo product attribute pages
  39. Template structure
  40. Custom Loop Using Shortcode in Custom Page Template
  41. Listing subpages title and content and styling the first iteration differently
  42. single.php – how to mark current page in the loop
  43. Cannot retieve the_content() and the_author() – both returning empty strings
  44. Get post id within comments loop
  45. How to order by multiple fields using standard query_posts?
  46. Get 1 more post in loop
  47. How to get the excerpt for is_home() outside of the loop isn’t working
  48. Make loop alphabetical
  49. How to check if a product is purchased within the last 365 days before displaying something
  50. How do I get my loop.php to properly paginate?
  51. Loop through ACF taxonomies and output associated posts
  52. How to get user ID’s from multiple usernames?
  53. How to display first post
  54. Pagination on custom post type on front-page.php
  55. get_template_part() Not Working in Loop
  56. get_delete_post_link() inside Loop stubbornly returns nothing
  57. Query posts only with actual text content (not including shortcode or images)
  58. CPT while loop not working
  59. Problem with Front-Page.php loading recent posts
  60. Comments_template doesn’t work
  61. Get markup for post without loading into the template
  62. Where to add offset?
  63. How to synchronize The Loop with the max post per page?
  64. How to add a byline to all the pages?
  65. How do i create a list-posts-page?
  66. Exclude posts based on an array
  67. Displaying member join date on page
  68. Ascending order has strange effects on wordpress loop
  69. List ALL posts by author on author archive
  70. How can I sort my loop based on meta data, using a form?
  71. get post images to link to external sources
  72. Using is_page_template inside loop
  73. How to add custom checkboxes from loop to WooCommerce product variation options?
  74. Count products with custom metadata field in an order
  75. How to show the last article in a different way in my blog?
  76. Can’t limit posts_per_page in loop
  77. Slug is changing when I use WP_Query in a metabox of a post
  78. How to execute a user loop with shortcode
  79. Custom loop not working in singular.php, working in other pages
  80. Loop Posts Outside of WordPress Installation
  81. Strange problem with wp_get_attachment_image_src database (db) query count
  82. How to include posts from one category, into a separate category of posts?
  83. how do i make this loop work
  84. Posts are being displayed from old to new, in ascending order
  85. Set loop format & have a loop inside other one
  86. Need a conditional to test if title of parent page matches title of child page
  87. Query counting excluded category on paged loop
  88. Using shortcode within shortcode with dynamic variable
  89. modify default HAVE_POSTS() Loop without inserting new variable (i.e. $loop->HAVE_POSTS)
  90. Why is my pagination showing up?
  91. Changing the loop w/o killing category links
  92. Randomly Display One Image From a 10-Image For Loop
  93. An action that runs after each post in the loop on index/archive page?
  94. Modify my code – which takes the first sentence of the post and use it as a h2 tag – to work outside the loop
  95. Getting post id in for loop
  96. Different style for first two (sticky) posts
  97. pull 500 post of many from database [duplicate]
  98. WordPress loop put title into variable
  99. How do I display 3 post each in a bootstrap carousel?
  100. Multiple loops using next_posts_link & previous_posts_link
Categories loop Tags get-post, loop, post-meta
Removing backstretch image from pages/posts
Is there any difference between below WP_Query code snippets?

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