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 7491193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:56:20+00:00 2026-05-29T15:56:20+00:00

Querying wordpress posts according to their tags can look like this (if I pieced

  • 0

Querying wordpress posts according to their tags can look like this (if I pieced it together correctly – I removed irrelevant parts from the query):

SELECT wposts.ID AS ID,
wposts.post_title, wposts.post_status, wposts.post_name,
tag_terms.term_id AS tag_id

FROM `wp_posts` AS wposts

INNER JOIN wp_term_relationships AS tag_term_relationships ON (wposts.ID = tag_term_relationships.object_id)
INNER JOIN wp_term_taxonomy AS tag_term_taxonomy ON (tag_term_relationships.term_taxonomy_id = tag_term_taxonomy.term_taxonomy_id AND tag_term_taxonomy.taxonomy = 'post_tag')
INNER JOIN wp_terms AS tag_terms ON (tag_term_taxonomy.term_id = tag_terms.term_id)

WHERE wposts.ID = '12345'

AND wposts.post_type = 'post'

AND wposts.post_status NOT LIKE 'private'

AND tag_terms.term_id = '55'

GROUP BY wposts.ID
ORDER BY wposts.post_date ASC

This should query all posts with tag id 55.

What I need to do is to filter out all posts that only contain this single tag, but no other tags.

So I want to show a post if it has the tags 23,34,55,67
But I don’t want to show the post if it has the tag 55 (and no other tags).
Posts that do not contain this certain tag should be included in the query as well.

How do I do this?

  • 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-29T15:56:21+00:00Added an answer on May 29, 2026 at 3:56 pm

    Try adding HAVING condition between GROUP BY and ORDER BY:

    ...
    GROUP BY wposts.ID
    HAVING COUNT( tag_terms.term_id ) <> 1
      OR MAX( tag_terms.term_id ) <> 55
    ORDER BY wposts.post_date ASC
    

    and change your WHERE condition to check only for post type and status.

    Also if you don’t select anything other than tag_id from wp_terms joining it is not necessary as you can just use term_id from wp_term_taxonomy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Querying a Nested Set Model table , here's the SQL... how can this be
i'm querying a database like this: SELECT * from Log WHERE cookieId IN (select
I am querying an HTML file with LINQ-to-XML. It looks something like this: <html>
Im not good in querying. I would like to ask if this query works.
When querying with LDAP against our Active Directory structure to look up user accounts,
I'm new to querying XML datatype in SQL Server 2005. Anyone can help me
I am querying database from my EJB Bean, which is DAO, my query look's
By querying the portal_languages tool I can get a list of language names: >>>
on querying a camle jpa component like <from uri=jpa:jpa.camel?consumer.query=select x from jpa.camel x/> <to
Querying by HQL with select a from Article a join a.tags t where t.name

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.