how to – Difficult MySQL Query on WordPress Database postmeta
Wordpress saves the authors via custom fields in postmeta and not in term taxonomy or whatever (the authors are not the same as the users that post the articles…). WordPress saves the post_views_count in the postmeta as well.
So the structure of postmeta is: metaid(auto increment), post_id, meta_key, meta_value.
For every post there is an entry with meta_key = autor & meta_value = autorid and meta_key = post_views_count & meta_value = intpostviews
The problem is that I need both entries in the same query…
I had a plugin counting the views as well so my previous query was this one (there was a table from the plugin which I could use for the query):
SELECT wpm.meta_value AS Autor, SUM(wpd.pageviews) AS views, Count(wp.id) AS Artikelanzahl, (SUM(wpd.pageviews) / Count(wp.id)) AS average FROM `wp_posts` wp JOIN wp_postmeta wpm ON (wpm.post_id = wp.ID) JOIN wp_popularpostsdata wpd ON (wpd.postid = wp.ID) WHERE MONTH(post_date) = MONTH(CURRENT_DATE()) AND YEAR(post_date) = YEAR(CURRENT_DATE()) AND post_status = 'publish' AND wpm.meta_key = 'autor' GROUP BY Autor ORDER BY average DESC
So I didn’t have the problem of using two different meta_key’s and values for the same query… Does anyone got a solution?