Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:57:45+00:00 2026-05-27T03:57:45+00:00

I need to agregate a table before joining it with other tables. wp_postmeta GROUP

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:57:46+00:00Added an answer on May 27, 2026 at 3:57 am

    I don’t think you should be doing a real join to wp_postmeta at all, since you don’t actually need anything from it; rather, you should perform a “semi-join”, using an IN or EXISTS clause. For example:

    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 IN
       (
          SELECT DISTINCT wp_postmeta.post_id
            FROM wp_postmeta
            WHERE 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'
       )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need an aggregate sum for a table column (grouped by other columns), but
I have a table with records which include a datetime column CreationDate. I need
I need to pull several rows from a table and process them in two
I want to join three tables and to calculate the Sum(Quantity) of the Table
I want to agregate updates of my tables into a feed. Let´s take sample
i have line by line data in a table and i need to net
i need your help in access db i have a table like this ID
How can I compress / aggregate / group a table with events dynamically over
I need to write a SQLlite query that will delete rows from a table
I need a SQL statement, the requirement is: there is a table, which has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.