Leave a Reply

3 comments

  1. It’s not a single step, but it’s easy to write & handle in code: get_post_custom() gives you all attached meta data. But be aware: Everything comes as array, even when it´s a single value. So

    // Ever entry comes like this.
    // Example single value for 'Hair color':
    Array(
        [0] => Array(
            'Brown'
        )
    )
    
  2. Here is a snippet of code source: WPSnipp.com

    Place this in your functions.php file…

    function get_post_meta_all($post_id){
        global $wpdb;
        $data   =   array();
        $wpdb->query("
            SELECT `meta_key`, `meta_value`
            FROM $wpdb->postmeta
            WHERE `post_id` = $post_id
        ");
        foreach($wpdb->last_result as $k => $v){
            $data[$v->meta_key] =   $v->meta_value;
        };
        return $data;
    }
    

    Or using get_post_custom() you can do this;

    Place this in your functions.php file…

    if ( !function_exists('base_get_all_custom_fields') ) {
        function base_get_all_custom_fields()
        {
            global $post;
            $custom_fields = get_post_custom($post->ID);
            $hidden_field = '_';
            foreach( $custom_fields as $key => $value ){
                if( !empty($value) ) {
                    $pos = strpos($key, $hidden_field);
                    if( $pos !== false && $pos == 0 ) {
                        unset($custom_fields[$key]);
                    }
                }
            }
            return $custom_fields;
        }
    }
    

    Then within your theme files you can do the following;

    $custom_fields = base_get_all_custom_fields();
    if( !empty($custom_fields) ) {
        print_r($custom_fields);
    }
    

    Source HERE