how to display custom data from custom post types

Prusprus picture Prusprus · Nov 4, 2011 · Viewed 28.8k times · Source

I've created a custom post type. It will load just fine in the Wordpress dashboard and I will be able to save it aswell. Now let's say it's a custom post type that contains data for a few strings and a few dates.

I want to be able to retrieve these custom post types (which i've done using WP_Query and specifying the post_type to the name of my custom post type). When i call print_r on the returned object, nowhere in the object is the custom data (strings and dates) stored. How would i retrieve these from the database?

I've looked around for hours and haven't found any approach to retrieving this data.

As requested: This is how the data is stored:

function update_obituary(){
    global $post;
    update_post_meta($post->ID, "first_name", $_POST["first_name"]);
    update_post_meta($post->ID, "last_name", $_POST["last_name"]);
    update_post_meta($post->ID, "birth_date", $_POST["birth_date"]);
    update_post_meta($post->ID, "death_date", $_POST["death_date"]);
    update_post_meta($post->ID, "publication_date", $_POST["publication_date"]);
}

This function is tied to the 'save_post' hook. The data will be redisplayed when i reopen the custom post type instance in edit mode. That means that it's stored in the database, right?

Answer

Johannes Pille picture Johannes Pille · Nov 5, 2011

If the metadata shows up when editing posts of the type, then yes, it must have been successfully stored in the DB.

There's two wp functions to retrieve the custom post type's metadata: get_post_custom_values and get_post_meta. The difference being, that get_post_custom_values can access non-unique custom fields, i.e. those with more than one value associated with a single key. You may choose to use it for unique fields also though - question of taste.

Assuming, that your post type is called "obituary":

// First lets set some arguments for the query:
// Optionally, those could of course go directly into the query,
// especially, if you have no others but post type.
$args = array(
    'post_type' => 'obituary',
    'posts_per_page' => 5
    // Several more arguments could go here. Last one without a comma.
);

// Query the posts:
$obituary_query = new WP_Query($args);

// Loop through the obituaries:
while ($obituary_query->have_posts()) : $obituary_query->the_post();
    // Echo some markup
    echo '<p>';
    // As with regular posts, you can use all normal display functions, such as
    the_title();
    // Within the loop, you can access custom fields like so:
    echo get_post_meta($post->ID, 'birth_date', true); 
    // Or like so:
    $birth_date = get_post_custom_values('birth_date');
    echo $birth_date[0];
    echo '</p>'; // Markup closing tags.
endwhile;

// Reset Post Data
wp_reset_postdata();

A word of caution, to avoid confusion: Leaving out the boolean in get_post_meta will make it return an array rather than a string. get_post_custom_values always returns an array, which is why, in the above example, we're echoing the $birth_date[0], rather than $birth_date.

Also I'm not 100% certain at the moment, whether $post->ID will work as expected in the above. If not, replace it with get_the_ID(). Both should work, one will for sure. Could test that, but saving myself the time...

For the sake of completeness, check the codex on WP_Query for more query arguments and correct usage.