Retrieving Metadata in WordPress by Web Design Company London

In the previous articles we have read that how to add, update, and delete metadata in WordPress, In this article you will review how to retrieve and display metadata. For retrieve and display the post metadata WordPress makes it easy. To display the custom metadata a good place of code that use it within the loop for a particular piece of content. The get_post_meta() function will use to retrieve the data:

<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>

These parameters are accept by this function:

  •  $post_id — This is use for the ID of the post to retrieve metadata for.
  •  $meta_key — This is use for the name of the metadata field.
  •  $single — This is use for A value identifying whether to return a single meta value field (false) or return an array of values (true). This parameter is set to false by default.

Here in the example retrieve and display the price for your product created in previous article:

$product_price = get_post_meta( 571, ‘prowp_price’, true );
echo ‘Price $’ .$product_price;

Against the product ID 571 the product price is retrieved and displayed.
Let’s suppose that you want to store various sizes for the product in the database. Instead of creating a separate metadata entry for each size of product, here you can create an array of size entries in a single metadata field see below:

add_post_meta( 571, ‘prowp_size’, ‘small’, false );
add_post_meta( 571, ‘prowp_size’, ‘medium’, false );
$product_size = get_post_meta( 571, ‘prowp_size’, false );
echo ‘<ul class=”product-size”>’;
foreach ( $product_size as $size ) {
echo ‘<li>’ .$size .'</li>’;
echo ‘</ul>’;

In the above example in first step you have to create the metadata entries for the product size. Unisg the add_post_meta() function. After that, set the meta key name to the same and the $unique parameter to false, so it will allow multiple entries under the same meta key of product. Then, the get_post_meta() function use to retrieve the product size you just set. In the code you notice the $single parameter is set to false, it will allows you to return all entries for prowp_size for product ID 571 as an array. Then through the loop the size array and display each product size. The get_post_custom() function is another powerful function for retrieving post metadata. For a particular post this function returns a multidimensional array of all metadata.

<?php get_post_custom( $post_id ); ?>

A single required parameter accept by this function, this is $post_id , this is the ID of the post whose custom fields will be retrieved or fetch.  Now let’s fetch and display all metadata entries for your product:

$product_metadata = get_post_custom( 571 );
foreach( $product_metadata as $name => $value ) {
echo ‘<strong>’ .$name .'</strong> => ‘;
foreach( $value as $nameAr => $valueAr ) {
echo ‘<br />’ .$nameAr.” => “;
echo var_dump( $valueAr );
echo ‘<br />’;

In the above example code will retrieve all metadata for product ID 571. That’s why because the value returned is a multidimensional array, for display all of the data you have to do multiple loops. Multiple pieces of metadata for a post if you are retrieving, It retrieves all metadata in a single database query instead of running separate queries for each piece of data requested so this is the optimized method.