Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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) # 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();
share|improve this answer
    
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 '12 at 5:57
    
Are all of the meta keys you are looking for created without a prepended underscore? –  Brian Fegter Jul 18 '12 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 '12 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. –  Brian Fegter Jul 18 '12 at 6:10
    
Check out my latest edit. That should work dandy. –  Brian Fegter Jul 18 '12 at 6:15

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.

share|improve this answer
    
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 '12 at 5:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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