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

WordPress loop by meta key that is an array? and how loop multiple arrays

You can’t order by values inside serialised arrays in post meta. When you store an array like this:

$value = [
    [
            'plan_name'  => 'name 1',
            'plan_price' => '1',
            'theType'    => 'yesPaid',
            'plan_space' => 'Unlimited',
    ],
    [
            'plan_name'  => 'name 2',
            'plan_price' => '3',
            'theType'    => 'yesFree',
            'plan_space' => '5000',
    ],
    [
            'plan_name'  => 'name2',
            'plan_price' => '5',
            'theType'    => 'yesPaid',
            'plan_space' => 'Unlimited',
    ],
]

This is stored in PHP like this:

a:3:{i:0;a:4:{s:9:"plan_name";s:6:"name 1";s:10:"plan_price";s:1:"1";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}i:1;a:4:{s:9:"plan_name";s:6:"name 2";s:10:"plan_price";s:1:"3";s:7:"theType";s:7:"yesFree";s:10:"plan_space";s:4:"5000";}i:2;a:4:{s:9:"plan_name";s:5:"name2";s:10:"plan_price";s:1:"5";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}}

As far as MySQL — which is responsible for querying posts — is concerned, this is just a long string. Indistinguihsable from a word or sentence that begins with a. It’s only turned back into an array by WordPress when the value is retrieved with get_post_meta().

So it’s not possible to sort by a property within an array, and if you attempt to sort by the meta value of this meta then it’s just going to sort as if this were a string.

If you need to sort this way then you need to find an alternate way of storing the data. I don’t have enough insight into your project to suggest anything specific, but you probably need a custom database table for these plans, which you’d then need to join and sort using the posts_join and posts_orderby hooks to modify the query SQL directly.

Related Posts:

  1. Order a WP_Query by meta value where the value is an array
  2. WP post meta – for loop inside for loop
  3. How to update custom fields using the wp_insert_post() function?
  4. How can merge two arrays values in one array and save in database
  5. How to display multiple Post meta_key/meta_values by SQL query
  6. How to use multiple query with same meta key
  7. How to update single value in multi dimensional Post Meta?
  8. How to loop over custom fields in a page template?
  9. Print html when custom field has value inside while loop
  10. Compare two meta key values against each other inside the get_posts array?
  11. How can update custom meta for all posts
  12. Looping through WP_Post Object
  13. Use ajax to update_post_meta
  14. how to increase custom post value by one most efficiently?
  15. Must Use Plugin Causing Query Error
  16. Create a random unique 6 digit number as custom field for custom post type
  17. How can I add multiple ‘tax_query’ arrays via a loop?
  18. How can I create a list of page titles from custom meta values?
  19. Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
  20. How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
  21. How can I add extra word in permalink when someone click download button?
  22. Checking array against author id in loop
  23. Advanced Custom Fields – display label and value only if value entered
  24. Is it possible to retrieve all posts with a certain value for metadata?
  25. Get post id within comments loop
  26. Non-Closing PHP Query in WordPress Loop
  27. Output meta into arrays
  28. how to get serialized post meta
  29. How to get the last category name of a child category?
  30. How to use two meta_compare in an array?
  31. Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
  32. Remove empty terms from array, sort alphabetically, update back to repeating field
  33. Only show first image in foreach loop
  34. Get all posts as an array ID => Name
  35. How to store multiple custom meta box
  36. How to add post meta in while loop?
  37. Add a custom class to the body tag using custom fields
  38. Define category ID using get_post_meta
  39. Hide a div when a custom field is empty
  40. Update value of a associative array with update_post_meta
  41. WordPress stripping out custom field tags
  42. Add custom fields from different posts
  43. Order category posts by last word of custom field
  44. Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
  45. Products listing check if meta checkbox is checked
  46. in_array not working on dev server but works on localhost
  47. How to update custom fields when post is published?
  48. Where does php code to load data go?
  49. ACF – Get ID of relationship field in flexible content
  50. Show the subcategory name based on current product
  51. Store custom field’s multiple values in one user meta key
  52. Remove item from post_meta array via AJAX
  53. When working with a post, almost all wp_postmeta are deleted
  54. Updating Lat and Lng of posts automatically gives sporadic results
  55. Efficient way of querying for a “fallback” post?
  56. WP_Post is not from correct array
  57. divide custom field values in div every two values
  58. Create Customization Controls from Array
  59. If custom field doesn’t exist, or exists and it’s true show title?
  60. How can I put a custom field as the link of a button shortcode?
  61. Metadata on the WP_Post object
  62. Grabbing value of input field inside of array
  63. How to initialise WP_Query on the basis of a specific meta_value and continue iterating rest?
  64. I want to show image from custom field image on my custom page template
  65. fetch from an external api call and display results in page
  66. Using a javascript file to access a get posts array
  67. Generate an array of parent/child page IDs, based on parent page name
  68. Show Custom Taxonomy Title
  69. spliting posts into two columns
  70. Exclude posts based on meta value
  71. Alter required message using comment form api
  72. Can I access a post meta field before the loop?
  73. Why does this update_post_meta function not delete the custom field itself?
  74. How to add div blocks after certain set of post
  75. Why is an array created in a function hooked to customize register populated when customizer is loaded but not when the front-end is loaded?
  76. How do I remove a category from a wordpress loop>
  77. Get data from PHP to JavaScript to set position of each post on front page
  78. Problem with custom loop and wp_list_pluck [closed]
  79. Update post meta – Custom field does not match meta-key
  80. First post article different on Archives template
  81. How to add title attribute to archive items
  82. Adding custom url to readmore link using get_permalink()
  83. How to Create Carousel Indicators in PHP Loop using wp_get_attachment_url function?
  84. Block error message in foreach loop when looping through ACF field
  85. Is there anyway I can call the year once?
  86. Unable to update the meta boxes with multiple fields
  87. How to display custom field from a gallery thumbnail?
  88. Display a single row from nested array of custom field data
  89. Div Missing In Custom Loop Query
  90. Reformat data within a nested array
  91. Outputting a custom field in PHP
  92. !in_array doesnt recognize category
  93. Custom fields randomly stop working
  94. Woocommerce linking variations
  95. Displaying custom meta box value in a custom post page
  96. How do you create a front end form that enables the editing of member-specific custom fields in WordPress?
  97. How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
  98. Saving meta box data from selected option’s value in database is not working
  99. Problem with custom user fields default value and retrieval
  100. Cannot Access ACF Field Values via my Plugin
Categories PHP Tags array, custom-field, loop, php, post-meta
Add term slug in URL of custom post type details page
Password not resetting on wordpress?

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