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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:46:47+00:00 2026-05-11T06:46:47+00:00

I have developed a taxonomy for a site that I’ve been working on that

  • 0

I have developed a taxonomy for a site that I’ve been working on that may be abusing the categorization system of wordpress- posts are categorized both under what topics they refer to (let’s say cats, dogs, monkeys) as well as what type of post it is (say, expert, organization, article). So I’d like to find all posts that are about cats and dogs and that are organizations. Something along the lines of IN (cats, dogs) AND IN (organizations)… at least how it makes sense to me, but I can’t figure out the right SQL syntax for the task.

Based on what I found in this article on wordpress.com, I’m building from the query below… but I’m not sure of the right syntax for how to say ‘I want something that belongs to (either category 1 or 2) and (belongs to category 3) (say, cat 1=cats, 2=dogs, 3=organizations).

This is probably really simple and i’ll be kicking myself when I get the response.

SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE $wpdb->term_taxonomy.term_id = 1,2,3 AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->postmeta.meta_value ASC 

Thanks!

  • 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. 2026-05-11T06:46:48+00:00Added an answer on May 11, 2026 at 6:46 am

    Since the two conditions exist on different rows of the term_taxonomy table, you have to join to that table twice in order to compare them in one row of the result set.

    Also I don’t think you need to use LEFT JOIN since you’re using conditions in the WHERE clause. Outer joins usually perform slower than inner joins.

    You can use table aliases so you don’t have to keep repeating the variable table names.

    So I haven’t test this, but the following should be closer to what you need:

    SELECT * FROM $wpdb->posts p  INNER JOIN $wpdb->postmeta m ON (p.ID = m.post_id)  INNER JOIN $wpdb->term_relationships r1 ON (p.ID = r1.object_id)  INNER JOIN $wpdb->term_taxonomy x1   ON (r1.term_taxonomy_id = x1.term_taxonomy_id)  INNER JOIN $wpdb->term_relationships r2 ON (p.ID = r2.object_id)  INNER JOIN $wpdb->term_taxonomy x2   ON (r2.term_taxonomy_id = x2.term_taxonomy_id) WHERE x1.term_id IN (1, 2) AND x1.taxonomy = 'category'  AND x2.term_id = 3 AND x2.taxonomy = 'category'  AND p.post_status = 'publish' ORDER BY m.meta_value ASC 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a web site that requires user registration and authentication for some
I have developed a Java server using Eclipse that accepts TCP socket connection from
I have developed an app that in testing has worked fine but when it
I have developed a behavior that changes the Clip property of the AssociatedObject. When
I have developed a web application that uses the jQuery DateTimePicker by Trent Richardson
I have developed a sample WCF REST service that accepts that creates an Order
I have developed a grails application that stores a great deal of information. Currently,
I have developed an application that is to be run on android Tablet as
I have developed an android app that mainly targets smartphones. However in tablet emulator
I have developed a web site and used update panel controls on all pages.

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.