I am using a wordpress plugin called “kf most read” which stores a count of how many times a post was read, and lets you output a list of most read posts.
This works well. The issue is, I am trying to pull the most read posts, but only the most read posts within the current category you are viewing.
I am close to clueless when it comes to sql.
Here us what the plugin is currently using to pull the most read posts:
$sql = “SELECT count(mr.post_ID) as totHits, p.ID, p.post_title from $wpdb->posts p JOIN {$wpdb->prefix}kf_most_read mr on mr.post_ID = p.ID where mr.hit_ts >= ‘”.(time() – ( 86400 * $period)).”‘ GROUP BY mr.post_ID order by totHits desc, ID ASC LIMIT $limit”;
How could I incorporate the below query which pulls from a specific category into the above?
$sql .= “LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)” ;
$sql .= “WHERE $wpdb->term_taxonomy.term_id IN ($currentcat)” ;
$sql .= “AND $wpdb->term_taxonomy.taxonomy = ‘category’” ;
Any Help on this would be much appreciated.
You need to merge the from + join sections together, and the where clauses together. Your original query is
and your extra clauses
so the merged query should be
(NB you can only have one
where
so the adding the second as anand
)i.e. something like
depending on how you like your line-breaks. Hope that works!
Actually I suspect you don’t really want a LEFT JOIN for the categories / taxonomy table but should have a plain JOIN instead. But I’m no WP schema expert.