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

Unique meta_key with array value vs repeated meta_key with single values

Neither option are the correct option for your use case

Your question is barking up the wrong tree, because you neglected to mention that you intend to search for these posts based on the values the meta holds.

If you’re displaying a post, you can use either method, though the first is cleaner/simpler to handle in code. get_post_meta is fast and relies on table indexes, and WP fetches the post meta in advance anyway.

The problem here is if you have thousands of post meta, or a handful of hyper big post meta, in the extreme cases this can cause you to run out of memory or prevent it being cached by object caches

But What If You Searched For Them?

Showing a list of users who liked the post is going to be fast, but showing a list of posts a user liked?

This is what would happen to your sites speed and performance:

enter image description here

Searching for posts by their post meta is very, very bad. I’ve seen sites running on huge servers crippled by just a handful of these. It’s a huge slowdown, the kind of query that could take 10-20 seconds if it finishes at all once you have more than a handful of posts.

In this case, you would face a new problem too, if you had chosen option 2, the very ability to find those posts would become an issue. Thus option 1 would be superior, but performance would still be terrible

A Marginally Superior Solution With the Opposite Tradeoff

Use user meta to store the post IDs, rather than post meta to store the user IDs. Still has issues, aka showing a list of users who liked or unlocked a post is now super expensive. But showing a user all the posts they’ve liked is fast.

The Real Solution, and a General Rule of Thumb

If you need to store something, and you need to search/find/filter or show posts that have this special information, use a taxonomy.

In this case, a taxonomy named unlocked_by, where the term slug/name is the user ID that liked it.

Now your query is super fast:

$q = new WP_Query([
    'unlocked_by' => get_current_user_id()
]);

In fact, it’s so fast, you don’t even need that query, WP created a post archive at /unlocked_by/2/ ( I’m user number 2 btw ), assuming you enabled that.

You could even add the parameter via pre_get_posts so it happens transparently.

The wp_get_object_terms and wp_get_object_terms functions are your friend here

Related Posts:

  1. Ordering posts by anniversary using only day and month
  2. Displaying posts with only upcoming dates according their custom field date value
  3. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  4. How to add a new meta key and assign timestamp to posts
  5. How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
  6. Ordering posts by custom field named “date” in backend
  7. Avoiding ACF get_field and returning to core WordPress function
  8. change order of images attached to post
  9. How to improve my non-unique metadata MySQL entries?
  10. WP Query Args – search by meta_key or title
  11. How to get custom post meta using REST API
  12. The “_encloseme” Meta-Key Conundrum
  13. Using get_post_meta with new_to_publish
  14. SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
  15. Matching Serialized Arrays with meta_query
  16. So much data in postmeta
  17. Getting attachments by meta value
  18. get_post_meta() unserialize issue – returns boolean(false)
  19. Allow user to create instances of custom field
  20. Save HTML formatted data to post meta using add_post_meta()
  21. Up/Down voting system for WordPress
  22. post meta data clearing on autosave
  23. Compare meta_query decimals not working right
  24. Loop through two different sets of custom fields
  25. Save attachment custom fields on front end
  26. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  27. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  28. Custom field value based on other custom field values
  29. meta_value_num sort glitch
  30. Saving custom image meta fields
  31. Sorting posts by custom fields in meta_query
  32. meta_query for a string inside a meta field containing a comma-separated list
  33. Get all meta keys assigned to a post type
  34. Complex WP_User_Query call fails on production server
  35. Combine multiple custom field values into single value
  36. How to query posts with certain custom meta data, and output Post data
  37. Orderby custom field meta value ASC and then by date DESC
  38. Can’t get post ID using wp_insert_post_data
  39. Unique Post Meta Values
  40. WP 3.1 meta_query for multiple custom field values
  41. Fetch Record based on meta key dates
  42. Add post meta fields, when creating a post using WordPress’ REST API
  43. Store array as single, serialized post meta field or many post meta fields?
  44. Custom Field Values not updating unless I click “Update” twice
  45. update_post_meta for repeated custom field [closed]
  46. Large AND OR query timing out
  47. Trying to update_post_meta function
  48. Use meta_query to get title of associated post
  49. Limits, not all post are showen when querying for posts by view count
  50. Display Custom Meta Box Field Only If Value is Present
  51. Stop sending email everytime I updates my post
  52. Best way to store everyday post views?
  53. get posts where a custom field contains a text
  54. shortcode in a custom metabox
  55. Unable to get specific value from post meta
  56. Problem with saving large amount of data in postmeta/usermeta
  57. Custom meta is not being saved
  58. How to get posts with a metadata numeric key greater than X?
  59. Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
  60. IF Custom field value equals ZERO
  61. How to use media upload on metabox post page without breaking TinyMCE?
  62. Let’s Create Custom Field Template Documentation
  63. Branch made by several custom values
  64. WordPress creates new lines in postmeta table on post update
  65. Meta_query ‘compare’ => ‘LIKE’ not working?
  66. User customising position of WordPress Featured Image
  67. Can I save post meta programatically without setting metaboxes?
  68. Insert image into sub-field with update_post_meta
  69. How to use conditional statement with custom field
  70. Meta box values are displayed on Custom Fields list. Is it possible to hide them?
  71. If metabox has content display content
  72. Display Meta Data with HTML?
  73. How can I add/update post meta in a admin menu page?
  74. Best way to sort estates and query them (for rent? yes/no. contains office space? yes/no)?
  75. Get author total post votes from post meta
  76. Group Posts By Custom Meta Value: Date
  77. Making WP_Query limit results by date before today where date is a meta_query
  78. How to display custom fields in hestia theme
  79. How to speed up post list slowed by update_meta_cache()?
  80. WordPress Blocks, setAttributes not saving
  81. Show posts from WP Custom Post Type selected from a field in a metabox
  82. Adding Facebook’s image and meta description retrieval capabilities to a WordPress post
  83. Why does my numeric meta query work only on one meta key and not the other?
  84. meta_query to check all custom fields
  85. Update custom field on page specific to logged in user
  86. Save, update, get and sanitize post meta as HTML not plain
  87. Create Meta boxes dynamically
  88. Saved Post Meta Array Returns as String
  89. Problem saving meta data
  90. Check if value exists before saving
  91. Get meta value when the page is a blog archive
  92. change attachment custom field onChange event
  93. Query Posts Via WordPress URL
  94. wp_postmeta are updated for only one page
  95. Meta_query not filtering posts
  96. Make Custom Fields Public in JSON – API
  97. Order by a meta field in query loop
  98. Dynamic background image used in css after selector
  99. Site uses wpdb to fetch meta_keys but just displays first meta_key from a page (the post uses the same meta_key “filmmaker” more than once)
  100. Query ‘orderby’ when there are multiple values for the same meta_key
Categories custom-field Tags custom-field, meta-query, post-meta
using jQuery on only one page
How to limit the number of Related Posts?

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