Display trending / popular posts

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

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

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

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.

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.

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

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

$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.

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.


Within the loop I set some basic items - you can change these to suit yourself.



The Order by has to be “meta_value_num”

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.


I added an <h5> with a span. For the span I added a ‘Post Field - Smart’ action.

The I picked the metadata I wanted to display - again if you change the name, change what you enter here.

This results in the following code:

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

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