How to select posts with specific tags/categories in WordPress












7















This is a very specific question regarding MySQL as implemented in WordPress.



I'm trying to develop a plugin which will show (select) posts that have specific 'tags' and belong to specific 'categories' (both multiple)



I was told it's impossible because the way categories and tags are stored:





  1. wp_posts contains a list of posts, each post have an "ID"


  2. wp_terms contains a list of terms (both categories and tags). Each term has a TERM_ID


  3. wp_term_taxonomy has a list of terms with their TERM_IDs and has a Taxonomy definition for each one of those (either a Category or a Tag)


  4. wp_term_relationships has associations between terms and posts


How can I join the tables to get all posts with tags "Nuclear" and "Deals" that also belong to the category "Category1"?










share|improve this question





























    7















    This is a very specific question regarding MySQL as implemented in WordPress.



    I'm trying to develop a plugin which will show (select) posts that have specific 'tags' and belong to specific 'categories' (both multiple)



    I was told it's impossible because the way categories and tags are stored:





    1. wp_posts contains a list of posts, each post have an "ID"


    2. wp_terms contains a list of terms (both categories and tags). Each term has a TERM_ID


    3. wp_term_taxonomy has a list of terms with their TERM_IDs and has a Taxonomy definition for each one of those (either a Category or a Tag)


    4. wp_term_relationships has associations between terms and posts


    How can I join the tables to get all posts with tags "Nuclear" and "Deals" that also belong to the category "Category1"?










    share|improve this question



























      7












      7








      7


      0






      This is a very specific question regarding MySQL as implemented in WordPress.



      I'm trying to develop a plugin which will show (select) posts that have specific 'tags' and belong to specific 'categories' (both multiple)



      I was told it's impossible because the way categories and tags are stored:





      1. wp_posts contains a list of posts, each post have an "ID"


      2. wp_terms contains a list of terms (both categories and tags). Each term has a TERM_ID


      3. wp_term_taxonomy has a list of terms with their TERM_IDs and has a Taxonomy definition for each one of those (either a Category or a Tag)


      4. wp_term_relationships has associations between terms and posts


      How can I join the tables to get all posts with tags "Nuclear" and "Deals" that also belong to the category "Category1"?










      share|improve this question
















      This is a very specific question regarding MySQL as implemented in WordPress.



      I'm trying to develop a plugin which will show (select) posts that have specific 'tags' and belong to specific 'categories' (both multiple)



      I was told it's impossible because the way categories and tags are stored:





      1. wp_posts contains a list of posts, each post have an "ID"


      2. wp_terms contains a list of terms (both categories and tags). Each term has a TERM_ID


      3. wp_term_taxonomy has a list of terms with their TERM_IDs and has a Taxonomy definition for each one of those (either a Category or a Tag)


      4. wp_term_relationships has associations between terms and posts


      How can I join the tables to get all posts with tags "Nuclear" and "Deals" that also belong to the category "Category1"?







      php mysql sql wordpress plugins






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Ijas Ameenudeen

      6,55732746




      6,55732746










      asked Aug 26 '08 at 14:29









      yoavfyoavf

      9,13293037




      9,13293037
























          6 Answers
          6






          active

          oldest

          votes


















          3














          I misunderstood you. I thought you wanted Nuclear or Deals. The below should give you only Nuclear and Deals.



          select p.*
          from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

          where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id

          and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id

          and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id

          and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
          and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
          and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')





          share|improve this answer

































            2














            What a gross DB structure.



            Anyway, I'd do something like this (note I prefer EXISTS to joins, but you can re-write them as joins if you like; most query analyzers will collapse them to the same query plan anyway). You may have to do some additional juggling one way or another to make it work...



            SELECT *
            FROM wp_posts p
            WHERE EXISTS( SELECT *
            FROM wp_term_relationship tr
            WHERE tr.object_id = p.id
            AND EXISTS( SELECT *
            FROM wp_term_taxonomy tt
            WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
            AND tt.taxonomy = 'category'
            AND EXISTS( SELECT *
            FROM wp_terms t
            WHERE t.term_id = tt.term_id
            AND t.name = "Category1"
            )
            )
            AND EXISTS( SELECT *
            FROM wp_term_taxonomy tt
            WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
            AND tt.taxonomy = 'post_tag'
            AND EXISTS( SELECT *
            FROM wp_terms t
            WHERE t.term_id = tt.term_id
            AND t.name = "Nuclear"
            )
            AND EXISTS( SELECT *
            FROM wp_terms t
            WHERE t.term_id = tt.term_id
            AND t.name = "Deals"
            )
            )
            )





            share|improve this answer































              1














              So I tried both options on my WordPress db. I looked for the category "Tech" in my posts with the tags "Perl" AND "Programming".



              Eric's worked once I added a missing comma in the initial select statement. It returned 3 records. The problem is that the section that is looking for the "post_tag" is actually working as an OR option. One of my posts only had one tag not both. Also it would be good to make the SELECT DISTINCT.



              I tried Matt's version, but it kept returning an empty set. I may try to "juggle" with it.






              share|improve this answer

































                1














                Try this:



                select p.*
                from wp_posts p,
                wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
                wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                where p.id = tr.object_id
                and t.term_id = tt.term_id
                and tr.term_taxonomy_id = tt.term_taxonomy_id

                and p.id = tr2.object_id
                and t2.term_id = tt2.term_id
                and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))


                Essentially I'm employing 2 copies of the pertinent child tables - terms, term_taxonomy, and term_relationship. One copy applies the 'Category1' restriction, the other the 'Nuclear' or 'Deals' restriction.



                BTW, what kind of project is this with posts all about nuclear deals? You trying to get us on some government list? ;)






                share|improve this answer

































                  0














                  Thanks @Eric it works! Just a few code corrections for future reference:




                  • the first select statements misses a coma after wp_term_relationship tr2

                  • In the same select statemt the following must be change:


                  wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

                  tr2


                  should be



                  wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

                  tr3





                  share|improve this answer































                    0














                    Really so great answer .. helped me a lot..



                    great bcoz., it gave me basic approach to build my complex query !



                    one small correction, for ready users like me :)



                    "wp_term_relationship" will give 'doesn't exist error'
                    .. use wp_term_relationships as it is the correct table name.



                    Thanks Eric






                    share|improve this answer























                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28196%2fhow-to-select-posts-with-specific-tags-categories-in-wordpress%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      6 Answers
                      6






                      active

                      oldest

                      votes








                      6 Answers
                      6






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      3














                      I misunderstood you. I thought you wanted Nuclear or Deals. The below should give you only Nuclear and Deals.



                      select p.*
                      from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
                      wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
                      wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                      where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id

                      and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                      and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id

                      and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                      and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
                      and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')





                      share|improve this answer






























                        3














                        I misunderstood you. I thought you wanted Nuclear or Deals. The below should give you only Nuclear and Deals.



                        select p.*
                        from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
                        wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
                        wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                        where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id

                        and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                        and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id

                        and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                        and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
                        and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')





                        share|improve this answer




























                          3












                          3








                          3







                          I misunderstood you. I thought you wanted Nuclear or Deals. The below should give you only Nuclear and Deals.



                          select p.*
                          from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
                          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
                          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                          where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id

                          and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                          and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id

                          and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                          and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
                          and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')





                          share|improve this answer















                          I misunderstood you. I thought you wanted Nuclear or Deals. The below should give you only Nuclear and Deals.



                          select p.*
                          from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
                          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
                          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                          where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id

                          and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                          and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id

                          and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                          and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
                          and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 6 '12 at 20:25









                          JimmyPena

                          7,73553656




                          7,73553656










                          answered Aug 27 '08 at 17:57









                          EricEric

                          2,00341932




                          2,00341932

























                              2














                              What a gross DB structure.



                              Anyway, I'd do something like this (note I prefer EXISTS to joins, but you can re-write them as joins if you like; most query analyzers will collapse them to the same query plan anyway). You may have to do some additional juggling one way or another to make it work...



                              SELECT *
                              FROM wp_posts p
                              WHERE EXISTS( SELECT *
                              FROM wp_term_relationship tr
                              WHERE tr.object_id = p.id
                              AND EXISTS( SELECT *
                              FROM wp_term_taxonomy tt
                              WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                              AND tt.taxonomy = 'category'
                              AND EXISTS( SELECT *
                              FROM wp_terms t
                              WHERE t.term_id = tt.term_id
                              AND t.name = "Category1"
                              )
                              )
                              AND EXISTS( SELECT *
                              FROM wp_term_taxonomy tt
                              WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                              AND tt.taxonomy = 'post_tag'
                              AND EXISTS( SELECT *
                              FROM wp_terms t
                              WHERE t.term_id = tt.term_id
                              AND t.name = "Nuclear"
                              )
                              AND EXISTS( SELECT *
                              FROM wp_terms t
                              WHERE t.term_id = tt.term_id
                              AND t.name = "Deals"
                              )
                              )
                              )





                              share|improve this answer




























                                2














                                What a gross DB structure.



                                Anyway, I'd do something like this (note I prefer EXISTS to joins, but you can re-write them as joins if you like; most query analyzers will collapse them to the same query plan anyway). You may have to do some additional juggling one way or another to make it work...



                                SELECT *
                                FROM wp_posts p
                                WHERE EXISTS( SELECT *
                                FROM wp_term_relationship tr
                                WHERE tr.object_id = p.id
                                AND EXISTS( SELECT *
                                FROM wp_term_taxonomy tt
                                WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                AND tt.taxonomy = 'category'
                                AND EXISTS( SELECT *
                                FROM wp_terms t
                                WHERE t.term_id = tt.term_id
                                AND t.name = "Category1"
                                )
                                )
                                AND EXISTS( SELECT *
                                FROM wp_term_taxonomy tt
                                WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                AND tt.taxonomy = 'post_tag'
                                AND EXISTS( SELECT *
                                FROM wp_terms t
                                WHERE t.term_id = tt.term_id
                                AND t.name = "Nuclear"
                                )
                                AND EXISTS( SELECT *
                                FROM wp_terms t
                                WHERE t.term_id = tt.term_id
                                AND t.name = "Deals"
                                )
                                )
                                )





                                share|improve this answer


























                                  2












                                  2








                                  2







                                  What a gross DB structure.



                                  Anyway, I'd do something like this (note I prefer EXISTS to joins, but you can re-write them as joins if you like; most query analyzers will collapse them to the same query plan anyway). You may have to do some additional juggling one way or another to make it work...



                                  SELECT *
                                  FROM wp_posts p
                                  WHERE EXISTS( SELECT *
                                  FROM wp_term_relationship tr
                                  WHERE tr.object_id = p.id
                                  AND EXISTS( SELECT *
                                  FROM wp_term_taxonomy tt
                                  WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                  AND tt.taxonomy = 'category'
                                  AND EXISTS( SELECT *
                                  FROM wp_terms t
                                  WHERE t.term_id = tt.term_id
                                  AND t.name = "Category1"
                                  )
                                  )
                                  AND EXISTS( SELECT *
                                  FROM wp_term_taxonomy tt
                                  WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                  AND tt.taxonomy = 'post_tag'
                                  AND EXISTS( SELECT *
                                  FROM wp_terms t
                                  WHERE t.term_id = tt.term_id
                                  AND t.name = "Nuclear"
                                  )
                                  AND EXISTS( SELECT *
                                  FROM wp_terms t
                                  WHERE t.term_id = tt.term_id
                                  AND t.name = "Deals"
                                  )
                                  )
                                  )





                                  share|improve this answer













                                  What a gross DB structure.



                                  Anyway, I'd do something like this (note I prefer EXISTS to joins, but you can re-write them as joins if you like; most query analyzers will collapse them to the same query plan anyway). You may have to do some additional juggling one way or another to make it work...



                                  SELECT *
                                  FROM wp_posts p
                                  WHERE EXISTS( SELECT *
                                  FROM wp_term_relationship tr
                                  WHERE tr.object_id = p.id
                                  AND EXISTS( SELECT *
                                  FROM wp_term_taxonomy tt
                                  WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                  AND tt.taxonomy = 'category'
                                  AND EXISTS( SELECT *
                                  FROM wp_terms t
                                  WHERE t.term_id = tt.term_id
                                  AND t.name = "Category1"
                                  )
                                  )
                                  AND EXISTS( SELECT *
                                  FROM wp_term_taxonomy tt
                                  WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                  AND tt.taxonomy = 'post_tag'
                                  AND EXISTS( SELECT *
                                  FROM wp_terms t
                                  WHERE t.term_id = tt.term_id
                                  AND t.name = "Nuclear"
                                  )
                                  AND EXISTS( SELECT *
                                  FROM wp_terms t
                                  WHERE t.term_id = tt.term_id
                                  AND t.name = "Deals"
                                  )
                                  )
                                  )






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Aug 26 '08 at 14:41









                                  Matt RogishMatt Rogish

                                  18.3k106889




                                  18.3k106889























                                      1














                                      So I tried both options on my WordPress db. I looked for the category "Tech" in my posts with the tags "Perl" AND "Programming".



                                      Eric's worked once I added a missing comma in the initial select statement. It returned 3 records. The problem is that the section that is looking for the "post_tag" is actually working as an OR option. One of my posts only had one tag not both. Also it would be good to make the SELECT DISTINCT.



                                      I tried Matt's version, but it kept returning an empty set. I may try to "juggle" with it.






                                      share|improve this answer






























                                        1














                                        So I tried both options on my WordPress db. I looked for the category "Tech" in my posts with the tags "Perl" AND "Programming".



                                        Eric's worked once I added a missing comma in the initial select statement. It returned 3 records. The problem is that the section that is looking for the "post_tag" is actually working as an OR option. One of my posts only had one tag not both. Also it would be good to make the SELECT DISTINCT.



                                        I tried Matt's version, but it kept returning an empty set. I may try to "juggle" with it.






                                        share|improve this answer




























                                          1












                                          1








                                          1







                                          So I tried both options on my WordPress db. I looked for the category "Tech" in my posts with the tags "Perl" AND "Programming".



                                          Eric's worked once I added a missing comma in the initial select statement. It returned 3 records. The problem is that the section that is looking for the "post_tag" is actually working as an OR option. One of my posts only had one tag not both. Also it would be good to make the SELECT DISTINCT.



                                          I tried Matt's version, but it kept returning an empty set. I may try to "juggle" with it.






                                          share|improve this answer















                                          So I tried both options on my WordPress db. I looked for the category "Tech" in my posts with the tags "Perl" AND "Programming".



                                          Eric's worked once I added a missing comma in the initial select statement. It returned 3 records. The problem is that the section that is looking for the "post_tag" is actually working as an OR option. One of my posts only had one tag not both. Also it would be good to make the SELECT DISTINCT.



                                          I tried Matt's version, but it kept returning an empty set. I may try to "juggle" with it.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited May 23 '17 at 12:24









                                          Community

                                          11




                                          11










                                          answered Aug 26 '08 at 23:14









                                          Scott GottreuScott Gottreu

                                          2,00742333




                                          2,00742333























                                              1














                                              Try this:



                                              select p.*
                                              from wp_posts p,
                                              wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
                                              wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                                              where p.id = tr.object_id
                                              and t.term_id = tt.term_id
                                              and tr.term_taxonomy_id = tt.term_taxonomy_id

                                              and p.id = tr2.object_id
                                              and t2.term_id = tt2.term_id
                                              and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                                              and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                                              and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))


                                              Essentially I'm employing 2 copies of the pertinent child tables - terms, term_taxonomy, and term_relationship. One copy applies the 'Category1' restriction, the other the 'Nuclear' or 'Deals' restriction.



                                              BTW, what kind of project is this with posts all about nuclear deals? You trying to get us on some government list? ;)






                                              share|improve this answer






























                                                1














                                                Try this:



                                                select p.*
                                                from wp_posts p,
                                                wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
                                                wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                                                where p.id = tr.object_id
                                                and t.term_id = tt.term_id
                                                and tr.term_taxonomy_id = tt.term_taxonomy_id

                                                and p.id = tr2.object_id
                                                and t2.term_id = tt2.term_id
                                                and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                                                and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                                                and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))


                                                Essentially I'm employing 2 copies of the pertinent child tables - terms, term_taxonomy, and term_relationship. One copy applies the 'Category1' restriction, the other the 'Nuclear' or 'Deals' restriction.



                                                BTW, what kind of project is this with posts all about nuclear deals? You trying to get us on some government list? ;)






                                                share|improve this answer




























                                                  1












                                                  1








                                                  1







                                                  Try this:



                                                  select p.*
                                                  from wp_posts p,
                                                  wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
                                                  wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                                                  where p.id = tr.object_id
                                                  and t.term_id = tt.term_id
                                                  and tr.term_taxonomy_id = tt.term_taxonomy_id

                                                  and p.id = tr2.object_id
                                                  and t2.term_id = tt2.term_id
                                                  and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                                                  and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                                                  and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))


                                                  Essentially I'm employing 2 copies of the pertinent child tables - terms, term_taxonomy, and term_relationship. One copy applies the 'Category1' restriction, the other the 'Nuclear' or 'Deals' restriction.



                                                  BTW, what kind of project is this with posts all about nuclear deals? You trying to get us on some government list? ;)






                                                  share|improve this answer















                                                  Try this:



                                                  select p.*
                                                  from wp_posts p,
                                                  wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
                                                  wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

                                                  where p.id = tr.object_id
                                                  and t.term_id = tt.term_id
                                                  and tr.term_taxonomy_id = tt.term_taxonomy_id

                                                  and p.id = tr2.object_id
                                                  and t2.term_id = tt2.term_id
                                                  and tr2.term_taxonomy_id = tt2.term_taxonomy_id

                                                  and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
                                                  and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))


                                                  Essentially I'm employing 2 copies of the pertinent child tables - terms, term_taxonomy, and term_relationship. One copy applies the 'Category1' restriction, the other the 'Nuclear' or 'Deals' restriction.



                                                  BTW, what kind of project is this with posts all about nuclear deals? You trying to get us on some government list? ;)







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jul 6 '12 at 20:26









                                                  JimmyPena

                                                  7,73553656




                                                  7,73553656










                                                  answered Aug 26 '08 at 15:29









                                                  EricEric

                                                  2,00341932




                                                  2,00341932























                                                      0














                                                      Thanks @Eric it works! Just a few code corrections for future reference:




                                                      • the first select statements misses a coma after wp_term_relationship tr2

                                                      • In the same select statemt the following must be change:


                                                      wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

                                                      tr2


                                                      should be



                                                      wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

                                                      tr3





                                                      share|improve this answer




























                                                        0














                                                        Thanks @Eric it works! Just a few code corrections for future reference:




                                                        • the first select statements misses a coma after wp_term_relationship tr2

                                                        • In the same select statemt the following must be change:


                                                        wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

                                                        tr2


                                                        should be



                                                        wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

                                                        tr3





                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          Thanks @Eric it works! Just a few code corrections for future reference:




                                                          • the first select statements misses a coma after wp_term_relationship tr2

                                                          • In the same select statemt the following must be change:


                                                          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

                                                          tr2


                                                          should be



                                                          wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

                                                          tr3





                                                          share|improve this answer













                                                          Thanks @Eric it works! Just a few code corrections for future reference:




                                                          • the first select statements misses a coma after wp_term_relationship tr2

                                                          • In the same select statemt the following must be change:


                                                          wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

                                                          tr2


                                                          should be



                                                          wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

                                                          tr3






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Aug 28 '08 at 11:22









                                                          yoavfyoavf

                                                          9,13293037




                                                          9,13293037























                                                              0














                                                              Really so great answer .. helped me a lot..



                                                              great bcoz., it gave me basic approach to build my complex query !



                                                              one small correction, for ready users like me :)



                                                              "wp_term_relationship" will give 'doesn't exist error'
                                                              .. use wp_term_relationships as it is the correct table name.



                                                              Thanks Eric






                                                              share|improve this answer




























                                                                0














                                                                Really so great answer .. helped me a lot..



                                                                great bcoz., it gave me basic approach to build my complex query !



                                                                one small correction, for ready users like me :)



                                                                "wp_term_relationship" will give 'doesn't exist error'
                                                                .. use wp_term_relationships as it is the correct table name.



                                                                Thanks Eric






                                                                share|improve this answer


























                                                                  0












                                                                  0








                                                                  0







                                                                  Really so great answer .. helped me a lot..



                                                                  great bcoz., it gave me basic approach to build my complex query !



                                                                  one small correction, for ready users like me :)



                                                                  "wp_term_relationship" will give 'doesn't exist error'
                                                                  .. use wp_term_relationships as it is the correct table name.



                                                                  Thanks Eric






                                                                  share|improve this answer













                                                                  Really so great answer .. helped me a lot..



                                                                  great bcoz., it gave me basic approach to build my complex query !



                                                                  one small correction, for ready users like me :)



                                                                  "wp_term_relationship" will give 'doesn't exist error'
                                                                  .. use wp_term_relationships as it is the correct table name.



                                                                  Thanks Eric







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Feb 5 '09 at 15:26







                                                                  Raghu





































                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28196%2fhow-to-select-posts-with-specific-tags-categories-in-wordpress%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      Homophylophilia

                                                                      Updating UILabel text programmatically using a function

                                                                      Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage