One of the top questions that I get asked is: “how can I use my custom fields within a Global Snippet?”. So it’s worth writing about it.
Where data is stored
Most of the plugins store data into the
wp_postmeta table. So the first thing to find out is where the plugin stores data exactly and in which format.
As you may know I also have a WordPress Rating Plugin that some of my customers are using. It stores data into the
wp_postmeta table to each post.
wpbph_rating_countfor the rating count; stored as a number.
wpbph_rating_averagefor the rating value; stored as a number.
wpbph_ratingsfor the good and bad ratings; stored as an array of values.
The format of the data
It’s important to know in which format the data is stored. If you don’t know that please ask the developer of the plugin you’re using. Scroll down to learn more about how to fetch more complex data from the database.
How to use simple post meta data
Let’s say I want the user to rate my blog posts. This is what I would do:
- I activate the Purple Heart Rating plugin for all posts.
- Open up the Global Snippet for an Article.
- Integrate the
aggregateRatingproperty into the global Article schema.
aggregateRating should be a
AggregateRating schema. In there you need a few sub-properties with the following values:
ratingValue: Post meta field “wpbph_rating_average”
bestRating: Direct Text Input “100”
reviewCount: Post meta field “wpbph_rating_count”
worstRating: Direct Text Input “0”
Note: Please note you maybe need to use
ratingCount instead of
reviewCount depending on what you want to do.
This is how it looks like:
How to use complex data
Of course some plugins store data in various formats. PHP (the programming language behind WordPress) allows to save objects and arrays. If a plugin stores data in one of the mentioned formats you can use arrows in the post meta field like shown below.
Using data from arrays
Let’s assume that I’m using a rating plugin that stores all the data as an array in the postmeta database table with the name
Array ( [count] => 1 [value] => 5 )
What you can now to is to use an arrow (
->) in the texarea field to tell SNIP that it has to search for the value in that array:
This is how it looks like:
Using data from objects
Of course you can also access data from objects:
Object ( [count] => 1 [value] => 5 )
It works the same way. You just use the arrow:
Going even deeper
Of course you can go even deeper. Let’s say you have an object like this:
object ( [whatever1] => 1 [whatever2] => 2 [whatever3] => 3 [rating] => Object ( [count] => 1 [value] => 5 ) )
You can access the rating date like this:
If it’s too complex
If the data structure is too complex you can always use SNIPs Hooks and build your own field type.