mysql - Selecting posts together with optional taxonomy -
i want select (wordpress) posts eventual brand
, brand_id
(both same taxonomy). if there no brand set, post should selected anyway these 2 fields set null
.
this query work, it's excluding posts missing brand:
select p.id post_id, p.post_title title, p.post_content body, brand.name brand, brand.term_id brand_id, unix_timestamp(p.post_date) date_added wp_posts p inner join wp_term_relationships brand_rel on brand_rel.object_id = p.id inner join wp_term_taxonomy brand_tax on brand_tax.term_taxonomy_id = brand_rel.term_taxonomy_id , brand_tax.taxonomy = "product_brand" inner join wp_terms brand on brand.term_id = brand_tax.term_id p.post_type = 'product' , p.post_status = 'publish' group post_id;
changing inner join
left join
, posts - none of brands (both brand
, brand_id
null
):
select p.id post_id, p.post_title title, p.post_content body, brand.name brand, brand.term_id brand_id, unix_timestamp(p.post_date) date_added wp_posts p left join wp_term_relationships brand_rel on brand_rel.object_id = p.id left join wp_term_taxonomy brand_tax on brand_tax.term_taxonomy_id = brand_rel.term_taxonomy_id , brand_tax.taxonomy = "product_brand" left join wp_terms brand on brand.term_id = brand_tax.term_id p.post_type = 'product' , p.post_status = 'publish' group post_id;
if remove group by
statement during left join
seem posts , without brand should, alot of duplicates of brand
, brand_id
fields null
.
all appreciated.
Comments
Post a Comment