如何获得本周访问量最大的帖子? [英] How to get most visited posts of the week?

查看:32
本文介绍了如何获得本周访问量最大的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户访问帖子时,我都会触发一个简单的函数,只需将 +1 添加到该帖子的单个自定义字段访问计数器字段即可.

Whenever a user visits a post, I have a simple function that triggers that simply adds +1 to a single custom field visit counter field for that post.

这意味着我可以使用 new WP_Query() 使用元值进行排序,轻松获得有史以来访问量最大的帖子,但我一生都无法找出一个好的仅获取最近 X 天访问量最大的帖子的方法.

This means I can easily get the most visited posts of all time using a new WP_Query() using the meta value to sort, but I can't for the life of me figure out a good way to only get the most visited posts from the last X days.

因为我只是将每个帖子的自定义字段增加 1,所以我知道没有对日期或任何内容的引用,但我无法想象一个好的编码解决方案来实现这一点.

As I'm simply increasing the custom field of each post by 1, I understand that there is no reference to dates or anything, but I can't imagine a good coding solution to make that happen.

是否有 WordPress 方法可以确定自定义字段在过去 7 天内更改了多少次?

Is there a WordPress way to determine how many times a custom field changed over the last 7 days?

推荐答案

解决方案是每天都有一个计数器.

A solution would be to have a counter per-day.

假设您今天有一个计数器 - 我们称之为 09052016,明天的另一个名为 10052016.然后要获得过去 7 天的观看次数,您只需将过去 7 天的计数器加在一起即可.

Say you have a counter for today - let's call it 09052016, and another one for tomorrow called 10052016. Then to get the views over the last 7 days, you just take the last 7 days' counters and add them together.

更简单的解决方案可能是逐步命名这些名称,因此您有 000001000002000003 等等.

An easier solution would possibly be to name these incrementally, so you have 000001, 000002, 000003 and so forth.

这样可以更轻松地编写一个 PHP 函数,让您可以使用 000182 和前面的 6 个计数器.

This would make it a lot easier to write a PHP function that would allow you to take say 000182 and the previous 6 counters.

也可以通过计算值来对过去 7 天的热门帖子进行排名:

Ranking top posts over the last 7 days would also be possible by calculating the value of the values:

x + (x-1) + (x-2) + (x-3) + (x-4) + (x-5) + (x-6)

我不确定这是否是最有效的解决方案.

I am not sure whether or not this is the most efficient solution.

您也可以每天有一个计数器,每周有一个计数器,1 天后减去 6 天前的观看量.

You could also have a counter per day and a counter per week, that after 1 day subtracts the amount of views from the day 6 days back.

这篇关于如何获得本周访问量最大的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆