M
M
makaravich2020-08-28 20:25:44
WordPress
makaravich, 2020-08-28 20:25:44

How to formulate an SQL query to fetch the most popular posts in WordPress?

The task is to display the most popular posts in WordPress for a month.
We have a standard table wp_posts in the database with posts. In this table, we are only interested in the ID
field. In addition, there is a wp_pp_stat table containing the post_id fields (they match the post IDs from the wp_posts table) and access_time - the access time to the post.
You need to create an SQL query that will return the 5 most viewed posts in the last month.
PS. Strongly do not kick, I'm new to SQL.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2020-08-28
@makaravich

select
  posts.*
  , count(*) as '# of posts'
from wp_pp_stat as stat
inner join wp_posts as posts
  on posts.id = stat.post_id
where stat.access_time > now() - interval 1 month
group by stat.post_id
order by count(*) desc
limit 5

There is no way to check, so try it yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question