5

Is there a way to dump a list of all meta keys being used by all posts belonging to a custom post type?

I.e., a post-type named foods, each food (ham, spaghetti, chicken), can have a different meta key, which is generated dynamically. I can't know which meta keys have been added, but I'd like to be able to use a list of all of them.

3 Answers 3

10

You can use a simple query to get a distinct list of user-entered meta_keys for a specific post type, then cache the results using the Transients API. The meta keys from this query will be those that do not start with an underscore or number.

function generate_foods_meta_keys(){
    global $wpdb;
    $post_type = 'foods';
    $query = "
        SELECT DISTINCT($wpdb->postmeta.meta_key) 
        FROM $wpdb->posts 
        LEFT JOIN $wpdb->postmeta 
        ON $wpdb->posts.ID = $wpdb->postmeta.post_id 
        WHERE $wpdb->posts.post_type = '%s' 
        AND $wpdb->postmeta.meta_key != '' 
        AND $wpdb->postmeta.meta_key NOT RegExp '(^[_0-9].+$)' 
        AND $wpdb->postmeta.meta_key NOT RegExp '(^[0-9]+$)'
    ";
    $meta_keys = $wpdb->get_col($wpdb->prepare($query, $post_type));
    set_transient('foods_meta_keys', $meta_keys, 60*60*24); # create 1 Day Expiration
    return $meta_keys;
}

To get a list of meta_keys in your template for generating your dropdown, use the following function:

function get_foods_meta_keys(){
    $cache = get_transient('foods_meta_keys');
    $meta_keys = $cache ? $cache : generate_foods_meta_keys();
    return $meta_keys;
}

$meta_keys = get_foods_meta_keys();
7
  • Works perfectly. Thanks. I'll need to figure out how to exclude some wordpress-included, behind the scenes meta values, but that's another question!
    – marctain
    Jul 18, 2012 at 5:57
  • Are all of the meta keys you are looking for created without a prepended underscore? Jul 18, 2012 at 6:00
  • no underscore. should they have one to make it easier? Or a prefix? Basically, all items I do not want are either numbers or start with an underscore. the rest are useful to me.
    – marctain
    Jul 18, 2012 at 6:06
  • Meta keys that start with an underscore are hidden from the custom fields interface. Let me see if I can whip up a query that adjusts to this. Jul 18, 2012 at 6:10
  • Check out my latest edit. That should work dandy. Jul 18, 2012 at 6:15
1

I believe you'd have to first do a query to gather up all the meta keys for all the custom post type into an array. So something like this:

$args = array(
    'post_type' => 'custom-post-type-name',
    );
$cpts = new WP_Query($args);
if($cpts->have_posts()) : while($cpts->have_posts() ) : $cpts->the_post();
    $meta_values[] = get_post_meta($post->ID);
endwhile; endif;

All your meta values will be stored in an the $meta_values array.

You could do a var_dump($meta_values) to look at the contents of the array to be sure.

1
  • works but gets heavy... if multiple posts have identical keys, you get the same key many times. I wonder if there's any way around this.
    – marctain
    Jul 18, 2012 at 5:37
0

Use get_post_custom to get all keys and values.

There is also a function called get_post_custom_keys() which you can use to retrieve all of the meta keys.

1
  • please read the question, it clearly asks for a solution for a cpt not a specific post! Feb 18, 2016 at 7:03

Your Answer

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

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