Add meta data as “flat” data to the post
The actual problem is, that your meta data comes back as array (with a single entry), when calling get_post_custom( $id );
for your post. Here’s a simple function, that adds all meta data attached to a post to your post object.
/**
* Merges custom/meta data with post data
* 1) get_post_custom() is cached from the WP_Query
* 2) The following line gets the first element from each meta array as it's only a single value
* It then merges the resulting flattened Array with the post object that gets casted to another Array
*
* @param array $query
* @return array result
*/
public function add_meta_data( $query )
{
foreach ( $query as $post )
$result[] = array_merge(
array_map( 'array_shift', get_post_custom( $post->ID ) )
,(array) $post
);
return $result;
}
How-to use
We then run the loop simply wrapped in a <table>
. You can add your <thead><th>etc.
before and the <tfoot>etc.
afterwards. Then just echo all your <tr><td>Cell</td>etc.
inside the loop.
// Before your loop:
global $wp_query;
// Merge the queried posts with their meta data, using our custom template tag/function
$query = add_meta_data( $wp_query->posts );
echo "<table>";
// Loop through our new query array
foreach ( $query as $post )
{
// This extracts your post object, so you don't have to save them in separate vars
// @see @link http://php.net/manual/de/function.extract.php
extract( $post, EXTR_SKIP );
echo "<tr>";
// Then simply call all your meta data like this:
echo "<td>{$hive}</td>";
// Then add other meta data here:
echo "<td>{$whatever}</td>";
echo "</tr>";
}
echo "</table>";