I need to agregate a table before joining it with other tables.
wp_postmeta GROUP BY wp_postmeta.post_id
Is it possible? Where should I put it in? Here is my code:
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-type'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3')
AND
(
wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value <> '111'
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value = '22'
OR wp_postmeta.meta_key <> 'include' AND wp_postmeta.meta_key <> 'exclude'
)
Found a new way of doing it while testing ruakh example. I don’t know why it works, but it does:
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-type'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3')
AND NOT wp_posts.id IN # ADDED NOT
(
SELECT wp_postmeta.post_id
FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'exclude' AND wp_postmeta.meta_value = '111' #SINCE NOT ABOVE I CHANGED <> to =
OR wp_postmeta.meta_key = 'include' AND wp_postmeta.meta_value <> '22' #SINCE NOT ABOVE I CHANGED = to <>
#I DELETED THIS LINE
)
Now the query gives back data if include or exclude isn’t set. And if they are it checks the ID.
Any comments on my change?
MORE IMPROVEMENTS:
SELECT wp_posts.post_content, wp_posts.ID, wp_terms.slug
FROM wp_posts
JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE wp_posts.post_type = 'my-type'
AND wp_posts.post_status = 'publish'
AND wp_terms.slug IN
('field1', 'field2', 'field3')
AND wp_posts.id NOT IN
(
SELECT wp_postmeta.post_id
FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'exclude'
AND wp_postmeta.meta_value IN
('$id', '$id,%', '%,$id,%', '%,$id')
OR wp_postmeta.meta_key = 'include'
AND wp_postmeta.meta_value NOT IN
('$id', '$id,%', '%,$id,%', '%,$id')
Example of what the wp_postmeta table might look like:
Senario 1 – Only pages with id 18 shall get the data:
meta_id post_id meta_key meta_value
1 30 include 18
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Senario 2 -Pages with id 18 shall not get the data:
meta_id post_id meta_key meta_value
1 30 exclude 18
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
Senario 1 – All pages shall get the data:
meta_id post_id meta_key meta_value
2 30 _edit_lock 1322225789:1
3 30 _edit_last 1
I don’t think you should be doing a real join to
wp_postmetaat all, since you don’t actually need anything from it; rather, you should perform a “semi-join”, using anINorEXISTSclause. For example: