*.meta type of placeholders - Display any value from WordPress

Among placeholders, there's one that has superpowers of accessing any data stored in meta table.

What is metadata?

A brief explanation for those, who aren't familiar with WordPress process of adding metadata to objects (posts, users, products). When it comes to post family (which consists of post, page, but also order and product for WooCommerce) the basic data is stored in wp_posts table in database, and any extra data is associated with post in wp_postmeta table.

Usually, the most interesting data is stored there. Think of it as container for anything related to your customers, orders or products. For example, billing email or billing name is stored in wp_postmeta table, exactly as metadata associated with the order you need.

Classic WordPress and WooCommerce metadata isn't the only use case of this behavior. Advanced Custom Fields also uses extensively meta table to save your data.

How to access metadata?

When you want to access metadata from PHP, i.e. in functions.php of your theme, usually you would write this as:

(This is only a mere example of retrieving post meta, don't actually copy that to your production code!)

/** \WP_Post $product Instance of WP_Post related with the product you need. */
function get_product_color_name( $product ) {
  $id = $product->ID;

  $meta = get_post_meta($id, '_product_color', true);

  return $meta;
}

As you have the ID of post you want metadata for, the most important element is the key to get it. In the example we've used _product_color as our meta key. Pay attention to the underscore starting the name of the key - saving any value this way treats meta as private. Private meta keys aren't easily accessed through standard WooCommerce methods (think of $product->get_meta('product_color')), that is because WooCommerce intentionally restricts your access to data saved as private and there's a good reason to do so.

In case of using our meta placeholders it doesn't matter whether it's private of public, as long as you remember to use proper name - never forget the underscore if data is saved with one!

Using *.meta placeholders

*.meta type of placeholders enables you to reach for any value you need, what makes it a perfect example for programmatically enhanced use cases.

Let's start simple, to acquaintance you with the usage. You can access standard WooCommerce data using this way. {{ order.billing_email }} can product the same output as {{ order.meta | key: '_billing_email' }}. As mentioned before, most of the data is stored in meta tables, so actually placeholder billing_email (or PHP call $order->get_billing_email()) in fact gets meta value associated with key _billing_email.

This type of placeholder has one required parameter: key. It works the same way as aforementioned PHP function get_post_meta call, where you need to specify the key of meta. Make sure to use correct name, with paying attention to the underscore, if it should be present.

Available *.meta placeholders

ShopMagic consists of multiple *.meta placeholders for different use cases. The most valuable is {{ order.meta }}, but there are also:

  • {{ product.meta }} - used in Review Requests add-on
  • {{ subscription.meta }} - used in WooCommerce Subscriptions add-on
  • {{ membership.meta }}- used in WooCommerce Memberships add-on
  • {{ bookings.meta }}- used in WooCommerce Bookings add-on

Usage constraints

*.meta placeholder restricts you to usage only of simple variable types in meta field. You cannot access an array through this placeholder, so metadata like following is forbidden.

$meta = [
  'name' => 'My Name',
  'phone' => '123123123',
  // more values in an array
];

It is because any complex data structure is serialized before saved to database and for the time being, it would be burdensome to access specific field from meta placeholder. Anyway, if you still feel need to access array data, we encourage you to create custom placeholder, following our guide, which will enable you to process the data, the way you want!

Where to find meta key?

Most of the time, you will likely use your own code solutions for saving meta value to the database. Then, you definitely know the best how your meta key is named, and it will be easy for you to reference it in placeholder call.

Yet, sometimes you may rely on external tools like Advanced Custom Fields, which already have its own way of storing data in database. In such cases there are two ways of finding out what key you should specify in placeholder.

First is simple - just get down to documentation of the tool you use, and try to find out how they save data. Here's an example for ACF.

Unfortunately, sometimes documentation lacks that information, then your best bet would be to dig into database. Try to find the ID of the item you are interested with and look through wp_postmeta table for any keys associated with specified ID.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us