NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Sep 03 2022
Estimated reading time : 0 minutes, 55 seconds - 122 words
Share the post "Count Post meta key by WordPress Post ID"
With this tutorial, we will create a query that will count the number of post meta key.
Let’s say we need to create a post meta views that will count the number of views per post ID.
First of all, we need to request the data used by fetching the meta key with get_post_meta.
With the example below, we have the post meta key called _post_like_count which will count the number of likes for each WordPress post.
Here’s the code snippet to inject into your single.php or custom single post type template.
<?php $podcast_date = get_post_meta(get_the_ID(), '_post_like_count', true); $count = $podcast_date; echo $count; if ($count > 1) { echo 'View(s)'; } else { echo 'view'; } ?>
Feels free to use our Via Post Meta Key widget which will automatically sort your posts by post meta key.
Via Post Meta Key Widget