2

I am going to implement a Hot and Trending section on a website. I have seen such sections on many popular websites but can't really figure out how exactly this works.

Although I assumed Hot section is most popular posts (based on post views) which I developed by a custom WP_Query. And it works.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'meta_key' => 'post_views',
    'orderby' => 'meta_value_num',
    'ignore_sticky_posts' => 1,
    'paged' => $paged,
);
$hot_query = new WP_Query( $args );

But I am stuck on Trending section. What exactly is the idea behind it for creating a custom query.

I want to know what is the best approach to get content in each section. I tried searching on Google for couple of days but could not really find anything useful.

EDIT

My post views function.

function set_post_views( $postID ) {
  if ( is_singular() ) {
    $count_key = 'post_views';
    $count = get_post_meta( $postID, $count_key, true );
    if ( $count=='' ) {
      $count = 0;
      delete_post_meta( $postID, $count_key );
      add_post_meta( $postID, $count_key, '0' );
    } else {
      $count++;
      update_post_meta( $postID, $count_key, $count );
    }
  }
}
1
  • I think your question has not only one good answer, it has several. It can be manually selected by editors, sorted by number of social shares, post views on last 24 hours, number of current readers ..... it can anything you can think about the definition of trending. I think it is up to you.
    – cybmeta
    Jan 2, 2015 at 9:33

5 Answers 5

3

In my personal opinion, this can get a bit resource intensive, so you will need to implement some type of cache system, or even look at transient

What you want to do is to extent your current post view function or even create one on the side for this. You would want to capture each view twice, once for normal page view as you are currently doing ($count_key) , and secondly for a custom view function using maybe $custom_count_key

Now, this is how $custom_count_key would work:

  • A page view is recorded when the page (post) is visited as per normal

  • This field is cleaned out (all data deleted) daily/weekly/etc depending on how you would like to define your Trending section. This again would set the value to 0 at the start of a new day/week/etc

What you will need to do:

  • As stated, either extend or write another page view function to store daily/weekly/etc views

  • Create a function that will run at a specific time daily/weekly/etc (whichever suite your needs better) which will delete/clean-out the values of that meta field.

  • Create your custom query to query an x amount of posts which has the highest amount of views for the specific day/week/etc. The posts been retrieved by this query will differ through out the day/week/etc as each post's views are increased due to page visits.

  • You can also save the results from a day/week/etc in the db for future reference before deleteting/cleaning out the current values at the end of that specific day/week/etc, but that is up to you and might even be just a waste of time and space

This is just a basic idea that you can use. I'm not in the position to code something like this now, but is would be really nice if you use this idea to post your final working solution to this

Good luck and all the best on this project

1
  • I think this is the closest I can get to this. And a practical example posted by Pieter that we can actually code without lots of efforts.
    – Robert hue
    Jan 5, 2015 at 18:16
2

The typical idea behind trending is content that is popular in short recent span of time. If you are only capturing total post views for content you likely won't have data to implement it.

For example:

  • Post A is 100 days old, had 100K views total, around 1K views a day.
  • Post B is 10 days old, had 10K views, 8K of them in last day.

A is long time popular, but B is trending at this moment (even though before that it had lower daily and all time traffic levels than A).

You would need to be able to tell “rolling” value, not just views in life time of the post, but how were those views distributed in time (at some passable level of precision).

3
  • Yes, that is what I was looking for. It is perfect but I don't really know how to set it up. I have been working with queries so that not an issue but for that we will need to assign a number/weight to each post based on this idea.
    – Robert hue
    Jan 2, 2015 at 18:45
  • @Roberthue you mention post_views field, but this is not something that WordPress does natively. So it's hard to guess what exactly is available in your installation.
    – Rarst
    Jan 2, 2015 at 19:18
  • I added my post views function in question. I also think it's really not WordPress can do by default. And my WordPress installation is very standard right now, no plugins.
    – Robert hue
    Jan 3, 2015 at 3:18
0

Trending is done by using between two values ,

Example

  • post views meta value>=1000
  • And the other post view meta value =<3000

And the hot page

  • Meta value =>3001 .

I cant post the code now I am from mobile , I used this in my comic site

3
  • Not really sure about that because I have seen many websites have same article on each section.
    – Robert hue
    Jan 2, 2015 at 6:33
  • Although this could be right because showing posts in both section will depend on how we set limit on post views for each. +1
    – Robert hue
    Jan 2, 2015 at 7:07
  • Okay this method is not a good one and in most cases will not serve the purpose. It wont feature older post if they gets boosted now.
    – Robert hue
    Jan 7, 2015 at 3:31
0

As @cybmeta said... You can define trending as you want. But I think you should consider using Social shares value for the trending posts. If you use any Social share plugin then you can extract the value from there.

2
  • I don't think differentiating numbers (post view count or share count) are too important because more of less they are connected. If a post is more shared then it will automatically get more post views.
    – Robert hue
    Jan 2, 2015 at 18:36
  • So I don't want to complicate it and concentrate on one easily obtainable number which is post views. And also if I fetch social shares every hour for 10-15k posts, it might slow down website.
    – Robert hue
    Jan 2, 2015 at 18:42
0

trending page query

$today = getdate();
$args = array(
          'meta_key'     => 'post_views',
          'meta_value'   => '1000',
          'meta_compare' => '>=',
          'orderby'    => 'meta_value_num',
          'ignore_sticky_posts' => 1,
          'paged' => $paged,
          'date_query' => array(
                array(
                        'year'  => $today['year'],
                        'month' => $today['mon'],
                         'day'   => $today['mday'],
    ),
),
);
$trenquery = new WP_Query( $args );

the hot page query should be the same but meta value 3000 or more. this code is TODAY trend . this code is tested in my site and working , and i also added by week and by month and all time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.