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. SQL query based on two different custom field values
  12. Using meta query (‘meta_query’) with a search query (‘s’)
  13. Can I exclude a post by meta key using pre_get_posts function?
  14. Custom post meta field effect on the performance on the post
  15. How to get custom post meta using REST API
  16. Difference between meta keys with _ and without _ [duplicate]
  17. Orderby meta_value only returns posts that have existing meta_key
  18. What is the index [0] for on post meta fields?
  19. What is “meta_input” parameter in wp_insert_post() used for?
  20. How to enable revisions for post meta data?
  21. The “_encloseme” Meta-Key Conundrum
  22. ORDER BY custom field value
  23. Best way to programmatically remove a category/term from a post
  24. Using get_post_meta with new_to_publish
  25. Can I query custom meta data through WP_Query
  26. Add custom fields to search
  27. SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
  28. Matching Serialized Arrays with meta_query
  29. Custom field metabox not showing in back-end
  30. Meta Query with AND & OR?
  31. So much data in postmeta
  32. Can I count the number of users matching a value in a multiple value key?
  33. Getting attachments by meta value
  34. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  35. How to hide meta box values from custom fields list?
  36. Auto sort the wp-admin post list by a meta key
  37. get_post_meta() unserialize issue – returns boolean(false)
  38. What is the advantage of the wp_options design pattern?
  39. Storing meta fields multiple times OR once with multi dimensional array?
  40. Allow user to create instances of custom field
  41. display specific custom fields
  42. How Can I save multiple records in same meta key?
  43. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  44. Meta keywords and descriptions plugin for manually editing meta for each page/post
  45. passing argument to get_template_part() or a better way to code
  46. Is it possible to store arrays in a custom field?
  47. Get updated meta data after save_post hook
  48. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  49. Save HTML formatted data to post meta using add_post_meta()
  50. Trying to perform complex custom field query with order by set to field value
  51. importing data from non-wordpress mysql db
  52. Is there a way to do multiple ordering on a multiple meta_query?
  53. Create meta boxes that don’t show in custom fields
  54. Transients vs CRON +Custom Fields: Caching Data Per Post
  55. Unable to save datetime custom meta field using update_post_meta() function
  56. Up/Down voting system for WordPress
  57. post meta data clearing on autosave
  58. WordPress Search Custom Meta Field Only
  59. Create custom field on post draft or publish?
  60. Display info from custom fields in all images’ HTML
  61. Compare meta_query decimals not working right
  62. WordPress altering my custom query, How to fix it?
  63. Calling Specific Pages with wp query Part II
  64. Loop through two different sets of custom fields
  65. get_post_meta fields don’t show up on posts page
  66. Update meta values with AJAX
  67. How to break meta values into different items and avoid duplicates?
  68. copy attachments to another post type and change attachment url
  69. Cannot edit post meta fields with rest API
  70. ajax delete value from custom field array
  71. Save attachment custom fields on front end
  72. Slow meta query with multi meta keys
  73. How to use pagination with get_post_meta
  74. Copying Custom Meta Values from existing post to a duplicate post
  75. Using OR in WP_Query negates the “NOT EXISTS” compare
  76. Add a post meta key and value only if it does not exist on the post
  77. How can I query on the year part of a complete date in a custom field?
  78. Move value of one custom field to another
  79. Order posts according to user defined order for meta values?
  80. How can I combine meta_query queries?
  81. Custom fields to save multiple values
  82. Custom fields: In what order are they saved into the DB?
  83. Function to change meta value in database for each post
  84. Get a post_id where meta_value equals something in a serialized meta_value field
  85. Get aggregate list of all custom fields for entire blog
  86. Difference between ‘LIKE’ and ‘IN’ in meta queries
  87. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  88. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  89. Sorting posts by multiple values, combined
  90. Custom field value based on other custom field values
  91. meta_value_num sort glitch
  92. WP_Query display next custom post from today’s date
  93. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  94. How to save a ToggleControl value in a meta field?
  95. WP_Meta_Query causing long-running MySQL queries
  96. Saving custom image meta fields
  97. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  98. What Is meta_id In wp_postmeta?
  99. Sorting posts by custom fields in meta_query
  100. How to display Meta Field Value?
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
  • 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