NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Easy

---- Last modified : Sep 03 2022

Estimated reading time : 0 minutes, 55 seconds - 122 words

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>