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

Get array of metakey in all posts

Your function is unreliable and totally overboard and really really expensive. Furthermore, as already stated by @MarkKaplun, you are not calling the_post() which causes the $post global not to be updated to the current post being looped through, so the value from get_post_meta() will always have the same value.

Although $post_id might work, it is one of those crappy variables set globally that is actually used to get the comments. It is better to use get_the_ID() or even $post->ID as you are inside the loop. For extra info, read this post

To solve your issue, I would just create a function with a custom SQL query to fetch all unique values from a specific meta key. Here is a function I have used on another answer

/**    
 * Description: Getting all the values associated with a specific custom post meta key, across all posts
 * Author: Chinmoy Paul
 * Author URL: http://pwdtechnology.com
 *
 * @param string $key Post Meta Key.
 *
 * @param string $type Post Type. Default is post. You can pass custom post type here.
 *
 * @param string $status Post Status like Publish, draft, future etc. default is publish
 *
 * @return array
 */
function get_unique_post_meta_values( $key = '', $type="post", $status="publish" ) 
{
    global $wpdb;
    if( empty( $key ) )
        return;
    $res = $wpdb->get_col( 
        $wpdb->prepare( 
            "SELECT DISTINCT pm.meta_value 
            FROM {$wpdb->postmeta} pm
            LEFT JOIN {$wpdb->posts} p 
            ON p.ID = pm.post_id
            WHERE pm.meta_key = '%s'
            AND p.post_status="%s"
            AND p.post_type="%s"", 
            $key, 
            $status, 
            $type 
        ) 
    );
    return $res;
}

You can then just use it as follow to get an array of unique meta value

$unique_values = get_unique_post_meta_values( 'json_id' );
?><pre><?php var_dump( $unique_values ); ?></pre><?php  

You can also build in some cache/transient system into the function to optimize it even more

Related Posts:

  1. How to get an array of menu items from the toolbar?
  2. Add options to featured image
  3. Change meta tags programatically
  4. Use a function to update post meta based on other post meta
  5. If function exists, and array is met, echo function?
  6. update_post_meta for custom field not working upon form submission
  7. Assign category using custom field?
  8. Convert User ID’s to User Names in a single.php file
  9. Defining a global array in functions.php?
  10. How to manage arrays from custom functions stored in functions.php?
  11. get_post_type() in in_array doesn’t work for some reason
  12. Simple custom theme option not being saved
  13. Can’t check if a post has thumbnail adding filter to get_post_metadata()
  14. Move Post to different category if post_meta field is 0 or is 2 days old?
  15. Sorting table function default
  16. What WordPress function to use to get meta value by using meta keys?
  17. Default Custom Field Value Automatically Update
  18. Post meta not updating
  19. Function to update post_meta based on existing post_meta
  20. Exclude from the query posts with meta_key and meta_value
  21. Prevent custom field from being updated after first publish
  22. User function to return multiple get_post_meta()
  23. Turn get_posts as string into an array for use in theme admin options
  24. How to echo the value of an array element using a function via a shortcode
  25. What are the meta fields for an attachment?
  26. issue with if/elseif in_array inside foreach loop display only one post
  27. Output comment_author in array
  28. How to avoid saving empty data to sql while using add_meta_box
  29. How to change text color depending on the number value (Using javascript)
  30. Override a Post’s URL with Advanced Custom Fields Function
  31. How to add custom metakey to shop_order page’s searching function?
  32. Update wp_postmeta table based on 2 keys
  33. How to create an array if ’empty’?
  34. blank page with wp_get_attachment
  35. Pass an argument into a function to extract from array
  36. Combine multiple menus using the filter wp_nav_menu_items
  37. Reference multiple style sheets, clearing styles for permalink page, custom fields for css
  38. How To Display Author Popup on Entry Meta (Genesis Framework)?
  39. Creating a “Related Meta” type field?
  40. Featured image on archive page based on post type
  41. Remove post from latest posts after a month only with certain tag
  42. save_post doesn’t correctly process function call with php class
  43. Code executes outside of Loop while same code gives ‘Uninitialized string offset’ notice inside a while loop
  44. How to assign results to variables?
  45. Get title from IDs in a string
  46. Return ACF Field value function
  47. Dynamically generated Navigation Diagram using Custom Fields
  48. update_post_meta not working in function
  49. How To Get WordPress Categories Last Update Date?
  50. How to add a meta information to the URL?
  51. Automatically add custom fields (post_meta) to all published posts, hourly, via wp_cron?
  52. Using array page name together with page id to deregister script
  53. update_post_meta after form is submited
  54. Add custom fields after post/page title
  55. Echo custom field value in shortcode function
  56. get_footer can’t find any variables set in functions.php
  57. Display post_meta-by_key on product catalogue
  58. trouble with passing class method data to outside function
  59. How to save custom made object in an array in a post meta field
  60. Adding and updating repeating custom field meta data
  61. Explode Array from Repeatable Custom Field
  62. Automatically delete posts that aren’t in an array
  63. Missing feature image link function
  64. What’s the difference between home_url() and site_url()
  65. Remove “Category:”, “Tag:”, “Author:” from the_archive_title
  66. get_template_directory_uri pointing to parent theme not child theme
  67. How to customize the_archive_title()?
  68. remove empty paragraphs from the_content?
  69. What is the “with_front” rewrite key?
  70. Why use if function_exists?
  71. How to extract data from a post meta serialized array?
  72. How to override parent functions in child themes?
  73. wp_enqueue_script was called incorrectly
  74. Add multiple custom fields to the general settings page
  75. Ajax call always returns 0
  76. 400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook
  77. How long does a deprecated function live in core?
  78. Solution to render Shortcodes in Admin Editor
  79. How to add a data attribute to a WordPress menu item
  80. What’s the difference between esc_html, esc_attr, esc_html_e, and so on?
  81. remove_action on after_setup_theme not working from child theme
  82. plugins_url vs plugin_dir_url
  83. Remove type attribute from script and style tags added by WordPress
  84. How to run a function every 5 minutes?
  85. Best way of passing PHP variable between partials?
  86. Upload Multiple Files With media_handle_upload
  87. How to display custom field in woocommerce orders in admin panel?
  88. Adding fields to the “Add New User” screen in the dashboard
  89. Issues with title-tag and document_title_parts
  90. How do I get the current edit page ID in the admin?
  91. How to check if a user exists by a given id
  92. Why isn’t is_page working when I put it in the functions.php file?
  93. Add tags to the section via functions.php
  94. Add image size if page template
  95. How to create a custom order status in woocommerce!
  96. Remove Actions/Filters added via Anonymous Functions
  97. Adding a second email address to a completed order in WooCommerce [closed]
  98. How to load parent_theme functions.php before child_theme?
  99. How to load scripts/styles specific for a page
  100. Programatically add options to “add new” custom field dropdown
Categories functions Tags array, functions, post-meta
Writing PHP code in pages without issue?
How to load different homepage on Mobile.?

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