0

I've got this query:

select p.ID, p.post_title, p.post_content, t.term_id, tt.taxonomy, t.name, t.slug, tt.count
from bdu_posts p
inner join bdu_term_relationships rel on p.ID = rel.object_id
inner join bdu_term_taxonomy tt on tt.term_taxonomy_id = rel.term_taxonomy_id
inner join bdu_terms t on tt.term_id = t.term_id
where p.post_type = "prodotto" and p.post_status = "publish"

then, basically, I need the same results grouped by t.term_id. Or, better, a subset of columns of the first query's results (the ones term-related), grouped by t.term_id.

In theory, what I want is simply to use the first query results as a base "table" for another query to get other sub-results.

I'm not really good in SQL. Probably the way would be things like table views, but I'm not sure how they work and especially how to use them with $wpdb.

Does anyone know? :)

  • How did you get that query? Did you hand write it? Does it come from WP_Query? What? – s_ha_dum Feb 14 '14 at 14:38
  • Hi, I did it by myself. In fact I already use it in production and I already solved my subquery problem, anyway, I'd like to have suggestions about my question, to have a more elegant solution for the subquery – Luca Reghellin Feb 14 '14 at 18:49
  • If you have hand written the SQL and want a pure SQL solution this is likely off-topic. If you want to reword the question so that it asks about using WordPress Core functions to do the same thing you might have more luck. – s_ha_dum Feb 14 '14 at 18:54
  • The question just starts with wpdb, that is a core WP class, and I'm asking how to achieve the goals using it. Also, $wpdb is made for using custom queries. – Luca Reghellin Feb 14 '14 at 19:01
  • $wpdb is a Core class. I can see that logic, but it is a thin wrapper. The solution as I see it is really just SQL and PHP. I think it might actually be easier with WP_Query and a filter or two. – s_ha_dum Feb 14 '14 at 19:21

Your Answer

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

Browse other questions tagged or ask your own question.