# Display trending / popular posts

**URL:** <https://forum.pinegrow.com/t/display-trending-popular-posts/4781>\
**Category:** General\
**Created:** [December 23, 2020, 11:16pm UTC](https://forum.pinegrow.com/t/display-trending-popular-posts/4781 "2020-12-23T23:16:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonlodder](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jasonlodder](https://forum.pinegrow.com/u/jasonlodder)\
**Post date:** [December 23, 2020, 11:16pm UTC](https://forum.pinegrow.com/t/display-trending-popular-posts/4781/1 "2020-12-23T23:16:44Z")

</div>

Hi everyone,

Does anyone know how to set up and display trending or popular posts on a WordPress theme with Pinegrow? I did a few google searches but can’t seem to find any tutorials?

Kind regards  
Jason Lodder

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 24, 2020, 11:08am UTC](https://forum.pinegrow.com/t/display-trending-popular-posts/4781/2 "2020-12-24T11:08:03Z")

</div>

How proficient are you with PHP? Fetching and displaying the posts with a custom loop is easy from within PG, but you will have to write a small function within your theme “functions.php” or you can choose to add it in a custom plugin.  
Bob

---

<div class="post-metadata">

**Author:** ![jasonlodder](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jasonlodder](https://forum.pinegrow.com/u/jasonlodder)\
**Post date:** [December 27, 2020, 10:42am UTC](https://forum.pinegrow.com/t/display-trending-popular-posts/4781/3 "2020-12-27T10:42:12Z")

</div>

Hi RobM,

I have done follow along and a very basic level of PHP in Codecademy. I would like to look at the option of adding it into the functions.php. Do you have any follow examples of this?

Thank you in advanced  
Jason Lodder

---

<div class="post-metadata">

**Author:** ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)\
**Post date:** [December 27, 2020, 1:55pm UTC](https://forum.pinegrow.com/t/display-trending-popular-posts/4781/4 "2020-12-27T13:55:25Z")

</div>

There are a few tutorials out there on the internet. The simplest version is to add a function that alters the post meta everytime the post is accessed. Note: this method will get fooled if you have a look ahead cache, or something that pre-fetches the article. Note, you should add this to the “inc/custom.php” file in your Pinegrow project, that way your theme will export correctly.

```auto
function count_post_visits()
{
	if (is_single()) {
		global $post;
		$views = $post->my_post_views;
		if ($views == '') {
			update_post_meta($post->ID, 'my_post_views', '1');
		} else {
			$views_no = intval($views);
			update_post_meta($post->ID, 'my_post_views', ++$views_no);
		}
	}
}
add_action('wp_head', 'count_post_visits);

```

So, this function fires if the user navigates to a post page as determined by `is_single()`. Note that this will also fire for pages and attachments - you can change this as desired.  
Then it grabs the `global $post` variable.  
`$views = $post->my_post_views;`  
From that it checks to see if that post has the metadata named `'my_post_views'` and what the value of the metadata is. If it doesn’t exist it will be created and an empty string will be returned.

```auto
			update_post_meta($post->ID, 'my_post_views', '1');

```

If that happens, the function will update the metadata with a value of 1

```auto
$views_no = intval($views);
update_post_meta($post->ID, 'my_post_views', ++$views_no);

```

else it will assign the current data to the `$views_no` variable and then update the post metadata with that number plus 1.

```auto
add_action('wp_head', 'count_post_visits);

```

Finally, we add our function to the ‘wp\_head’ hook.

Back in Pinegrow we have to set up a loop and a potentially a display of our page views. You can figure out your best use, but I set the loop up on an article. So, with the article selected I selected “The Loop” from the WordPress actions.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/8/8942b1ab6715bfe5197f1f00642b86684f8d5654.png)  
Within the loop I set some basic items - you can change these to suit yourself.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/e/e4baab30f6cdc63dc3049fb29aa7e54f87334a9e.png)  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/0/0680f3bc924e16ea9bc894bd27da720e0c006194.png)  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/2/24907581a8da235a603859c2722cdcd806592e9b.png)  
The Order by has to be “meta\_value\_num”  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/0/0dd059be7771b62c836b965bb046616f339328b9.png)  
If you choose to change your metadata name you should also change it here.

So, that is it for the loop. Next to show the page views.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/d/d874088b39fa9a9b0a1fb911bd51ebd1733876f6.png)  
I added an `<h5>` with a span. For the span I added a ‘Post Field - Smart’ action.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/d/dd99afa31ba6419d5e5e97b60c26d0e4e55cfd88.png)  
The I picked the metadata I wanted to display - again if you change the name, change what you enter here.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/f/f69fd582989daff0b1a2e733cfdedf43b8525ba7.png)  
This results in the following code:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/1/15538d75cc1ee1f94116e1cd02bfda7d47c46301.png)

If you need anything explained further, just reach out. Note- this function will favor older posts. You could change the function to also store a date and make you loop a little more complicated to also filter the posts by when it was last viewed. I think - but I’m not an expert - that you have to hand-edit the loop args in their own php block in order to sort by more than one metadata value.

Cheers,  
Bob

---

<div class="post-metadata">

**Author:** ![red-rosefields](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/red-rosefields/32/5392_2.png) [@red-rosefields](https://forum.pinegrow.com/u/red-rosefields)\
**Post date:** [December 27, 2020, 10:41pm UTC](https://forum.pinegrow.com/t/display-trending-popular-posts/4781/5 "2020-12-27T22:41:29Z")

</div>

> **[WordPress Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)**
>
> A highly customizable, easy-to-use popular posts widget!

How about using “Popular” post plugin and using Shortcodes? Not sure it will work fine with PG but…
