I need to get all meta keys/custom fields that are assigned to a post type.

I don't want to get the post_meta values for post_meta assigned to a particular post, or to all posts of a post type.

But, I want to get all possible custom fields that are 'assigned' to a post type.

I have looked and I am starting to worry that it's not possible, as maybe post_meta isn't 'registered' but only appears in the database when a post is saved?

I want to get all post meta information for a post type, in the same way I can get all taxonomies' information, assigned to a post type.

I want to be able to do:


and get something like:

array('custom_meta_key_1', 'custom_meta_key_2);

... regardless of whether or not there is even one single existing post of that post type.

Please tell me it's possible (and how to do it :))?


3 Answers 3


Answer by @Gareth Gillman definitely works but its an expensive query if we have hundreds of posts.

The function below is less expensive and using the wp native function get_post_custom_keys()

if ( ! function_exists( 'us_get_meta_keys_for_post_type' ) ) :

    function us_get_meta_keys_for_post_type( $post_type, $sample_size = 5 ) {

        $meta_keys = array();
        $posts     = get_posts( array( 'post_type' => $post_type, 'limit' => $sample_size ) );

        foreach ( $posts as $post ) {
            $post_meta_keys = get_post_custom_keys( $post->ID );
            $meta_keys      = array_merge( $meta_keys, $post_meta_keys );

        // Use array_unique to remove duplicate meta_keys that we received from all posts
        // Use array_values to reset the index of the array
        return array_values( array_unique( $meta_keys ) );



You may use the $sample_size param to include more posts in the loop.

Hope it helps someone.


query all posts in the post type and then get the meta keys from the posts e.g.

Not tested, may need some amendments

$meta_fields = array();
$the_query = new WP_Query( 'post_type=posttype' );
if ( $the_query->have_posts() ) {
 while ( $the_query->have_posts() ) {

  $meta_array = get_post_meta( get_the_ID() );
  foreach( $meta_array as $meta) {
   $meta_fields = $meta[];


You can then do whatever you want with the $meta_fields variable

  • I need be able to get all custom fields' information even if there aren't any existing posts of the post type
    – yolo191919
    Commented Dec 16, 2016 at 18:42
  • I don't think you can get post meta fields without a post id - developer.wordpress.org/reference/functions/get_post_meta The ID is a required field, so if you believe there are fields set outside of the posts then you would need to find another function to grab that data. Commented Dec 16, 2016 at 18:45
  • get_post_meta() is for getting the meta value knowing the meta _key. I want to get all meta_keys assigned to a post type, without knowing anything but the post type
    – yolo191919
    Commented Dec 16, 2016 at 18:51
  • I think I've figured it out... by getting all registered meta_boxes. I'll post back if that's it
    – yolo191919
    Commented Dec 16, 2016 at 18:52
  • Nope I'm wrong, can't because global $wp_meta_boxes is only accessible from certain places :(
    – yolo191919
    Commented Dec 16, 2016 at 19:05

I believe that get_registered_meta_keys will do the trick. Invoke it like this, get_registered_meta_keys( 'post', $post_type ), and it'll return an associative array of all of the meta keys and the data defines them. You can call array_keys( get_registered_meta_keys( 'post', $post_type ) ) if you just want the keys.

I think that this is correct because the source for register_post_meta shows that it simply defines an 'object_subtype' key and then delegates to register_meta( 'post', $meta_key, $args ). The $object_type arg and object_subtype key that are used in register_meta seem to line-up exactly with the args that get_registered_meta_keys accepts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.