Multiple Meta_Key Select on on Wordpress database












0














I've developed a query which selects events from Wordpress. I am using the where clause to select where the meta_value of the meta_key eventstartdate is after today.



The issue I'm having now is, I also want filter on a second meta_value that being from the meta_key '_VenueCity'.



I have tried aliasing the wp_postmeta table and doing a where on the meta_key but I think I'm missing a join.



This is the code that works without my additional code to get it to work. Can any one advise on how I get this to work?



SELECT

`wp_posts`.`ID` AS `EventID`,
`wp_posts`.`post_parent` AS `SeriesID`,
`wp_posts`.`post_title` AS `EventTitle`,
`wp_posts`.`post_content` AS `EventDescription`,
`wp_posts`.`post_excerpt` AS `EventSummary`,
`wp_posts`.`post_name` AS `EventSlug`,
min(`wp_postmeta`.`meta_value`) AS `EventStartDate`,
max(`tribe_event_end_date`.`meta_value`) AS `EventEndDate`,
`wp_posts`.`guid` AS `GUID`

FROM ((`wp_posts`
JOIN `wp_postmeta` ON
(
(`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
))

LEFT JOIN `wp_postmeta` `tribe_event_end_date` ON
(
(
(`wp_posts`.`ID` = `tribe_event_end_date`.`post_id`) AND
(`tribe_event_end_date`.`meta_key` = '_EventEndDate')
)
))


WHERE
(
(`wp_postmeta`.`meta_key` = '_EventStartDate') AND
(`wp_posts`.`post_type` = 'tribe_events') AND
(`wp_posts`.`post_status` = 'publish') AND
(`tribe_event_end_date`.`meta_value` >= CURDATE())
)

GROUP BY
`wp_posts`.`ID`
ORDER BY
`EventStartDate`,
`wp_posts`.`post_date`;









share|improve this question





























    0














    I've developed a query which selects events from Wordpress. I am using the where clause to select where the meta_value of the meta_key eventstartdate is after today.



    The issue I'm having now is, I also want filter on a second meta_value that being from the meta_key '_VenueCity'.



    I have tried aliasing the wp_postmeta table and doing a where on the meta_key but I think I'm missing a join.



    This is the code that works without my additional code to get it to work. Can any one advise on how I get this to work?



    SELECT

    `wp_posts`.`ID` AS `EventID`,
    `wp_posts`.`post_parent` AS `SeriesID`,
    `wp_posts`.`post_title` AS `EventTitle`,
    `wp_posts`.`post_content` AS `EventDescription`,
    `wp_posts`.`post_excerpt` AS `EventSummary`,
    `wp_posts`.`post_name` AS `EventSlug`,
    min(`wp_postmeta`.`meta_value`) AS `EventStartDate`,
    max(`tribe_event_end_date`.`meta_value`) AS `EventEndDate`,
    `wp_posts`.`guid` AS `GUID`

    FROM ((`wp_posts`
    JOIN `wp_postmeta` ON
    (
    (`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
    ))

    LEFT JOIN `wp_postmeta` `tribe_event_end_date` ON
    (
    (
    (`wp_posts`.`ID` = `tribe_event_end_date`.`post_id`) AND
    (`tribe_event_end_date`.`meta_key` = '_EventEndDate')
    )
    ))


    WHERE
    (
    (`wp_postmeta`.`meta_key` = '_EventStartDate') AND
    (`wp_posts`.`post_type` = 'tribe_events') AND
    (`wp_posts`.`post_status` = 'publish') AND
    (`tribe_event_end_date`.`meta_value` >= CURDATE())
    )

    GROUP BY
    `wp_posts`.`ID`
    ORDER BY
    `EventStartDate`,
    `wp_posts`.`post_date`;









    share|improve this question



























      0












      0








      0







      I've developed a query which selects events from Wordpress. I am using the where clause to select where the meta_value of the meta_key eventstartdate is after today.



      The issue I'm having now is, I also want filter on a second meta_value that being from the meta_key '_VenueCity'.



      I have tried aliasing the wp_postmeta table and doing a where on the meta_key but I think I'm missing a join.



      This is the code that works without my additional code to get it to work. Can any one advise on how I get this to work?



      SELECT

      `wp_posts`.`ID` AS `EventID`,
      `wp_posts`.`post_parent` AS `SeriesID`,
      `wp_posts`.`post_title` AS `EventTitle`,
      `wp_posts`.`post_content` AS `EventDescription`,
      `wp_posts`.`post_excerpt` AS `EventSummary`,
      `wp_posts`.`post_name` AS `EventSlug`,
      min(`wp_postmeta`.`meta_value`) AS `EventStartDate`,
      max(`tribe_event_end_date`.`meta_value`) AS `EventEndDate`,
      `wp_posts`.`guid` AS `GUID`

      FROM ((`wp_posts`
      JOIN `wp_postmeta` ON
      (
      (`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
      ))

      LEFT JOIN `wp_postmeta` `tribe_event_end_date` ON
      (
      (
      (`wp_posts`.`ID` = `tribe_event_end_date`.`post_id`) AND
      (`tribe_event_end_date`.`meta_key` = '_EventEndDate')
      )
      ))


      WHERE
      (
      (`wp_postmeta`.`meta_key` = '_EventStartDate') AND
      (`wp_posts`.`post_type` = 'tribe_events') AND
      (`wp_posts`.`post_status` = 'publish') AND
      (`tribe_event_end_date`.`meta_value` >= CURDATE())
      )

      GROUP BY
      `wp_posts`.`ID`
      ORDER BY
      `EventStartDate`,
      `wp_posts`.`post_date`;









      share|improve this question















      I've developed a query which selects events from Wordpress. I am using the where clause to select where the meta_value of the meta_key eventstartdate is after today.



      The issue I'm having now is, I also want filter on a second meta_value that being from the meta_key '_VenueCity'.



      I have tried aliasing the wp_postmeta table and doing a where on the meta_key but I think I'm missing a join.



      This is the code that works without my additional code to get it to work. Can any one advise on how I get this to work?



      SELECT

      `wp_posts`.`ID` AS `EventID`,
      `wp_posts`.`post_parent` AS `SeriesID`,
      `wp_posts`.`post_title` AS `EventTitle`,
      `wp_posts`.`post_content` AS `EventDescription`,
      `wp_posts`.`post_excerpt` AS `EventSummary`,
      `wp_posts`.`post_name` AS `EventSlug`,
      min(`wp_postmeta`.`meta_value`) AS `EventStartDate`,
      max(`tribe_event_end_date`.`meta_value`) AS `EventEndDate`,
      `wp_posts`.`guid` AS `GUID`

      FROM ((`wp_posts`
      JOIN `wp_postmeta` ON
      (
      (`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
      ))

      LEFT JOIN `wp_postmeta` `tribe_event_end_date` ON
      (
      (
      (`wp_posts`.`ID` = `tribe_event_end_date`.`post_id`) AND
      (`tribe_event_end_date`.`meta_key` = '_EventEndDate')
      )
      ))


      WHERE
      (
      (`wp_postmeta`.`meta_key` = '_EventStartDate') AND
      (`wp_posts`.`post_type` = 'tribe_events') AND
      (`wp_posts`.`post_status` = 'publish') AND
      (`tribe_event_end_date`.`meta_value` >= CURDATE())
      )

      GROUP BY
      `wp_posts`.`ID`
      ORDER BY
      `EventStartDate`,
      `wp_posts`.`post_date`;






      mysql sql wordpress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 20:59









      Valerian Pereira

      452311




      452311










      asked Nov 23 '18 at 14:39









      kil lie

      3218




      3218
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
          You should also consider what joins you want to use.



          SELECT
          p.post_title,
          pm1.meta_value,
          pm2.meta_value

          FROM wp_posts as p

          INNER JOIN wp_postmeta as pm1
          ON p.ID = pm1.post_id

          INNER JOIN wp_postmeta as pm2
          ON p.ID = pm2.post_id

          WHERE
          pm1.meta_key = '_my_postmeta_field1'
          AND
          pm2.meta_key <> '_not_this_field'





          share|improve this answer





















          • Thank you for your response. Can my existing query be modified to include the new query? I need the existing code to still work.
            – kil lie
            Nov 26 '18 at 11:06











          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%2f53448688%2fmultiple-meta-key-select-on-on-wordpress-database%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
          You should also consider what joins you want to use.



          SELECT
          p.post_title,
          pm1.meta_value,
          pm2.meta_value

          FROM wp_posts as p

          INNER JOIN wp_postmeta as pm1
          ON p.ID = pm1.post_id

          INNER JOIN wp_postmeta as pm2
          ON p.ID = pm2.post_id

          WHERE
          pm1.meta_key = '_my_postmeta_field1'
          AND
          pm2.meta_key <> '_not_this_field'





          share|improve this answer





















          • Thank you for your response. Can my existing query be modified to include the new query? I need the existing code to still work.
            – kil lie
            Nov 26 '18 at 11:06
















          0














          I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
          You should also consider what joins you want to use.



          SELECT
          p.post_title,
          pm1.meta_value,
          pm2.meta_value

          FROM wp_posts as p

          INNER JOIN wp_postmeta as pm1
          ON p.ID = pm1.post_id

          INNER JOIN wp_postmeta as pm2
          ON p.ID = pm2.post_id

          WHERE
          pm1.meta_key = '_my_postmeta_field1'
          AND
          pm2.meta_key <> '_not_this_field'





          share|improve this answer





















          • Thank you for your response. Can my existing query be modified to include the new query? I need the existing code to still work.
            – kil lie
            Nov 26 '18 at 11:06














          0












          0








          0






          I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
          You should also consider what joins you want to use.



          SELECT
          p.post_title,
          pm1.meta_value,
          pm2.meta_value

          FROM wp_posts as p

          INNER JOIN wp_postmeta as pm1
          ON p.ID = pm1.post_id

          INNER JOIN wp_postmeta as pm2
          ON p.ID = pm2.post_id

          WHERE
          pm1.meta_key = '_my_postmeta_field1'
          AND
          pm2.meta_key <> '_not_this_field'





          share|improve this answer












          I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
          You should also consider what joins you want to use.



          SELECT
          p.post_title,
          pm1.meta_value,
          pm2.meta_value

          FROM wp_posts as p

          INNER JOIN wp_postmeta as pm1
          ON p.ID = pm1.post_id

          INNER JOIN wp_postmeta as pm2
          ON p.ID = pm2.post_id

          WHERE
          pm1.meta_key = '_my_postmeta_field1'
          AND
          pm2.meta_key <> '_not_this_field'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 15:02









          Hans-Eric Lippke

          7917




          7917












          • Thank you for your response. Can my existing query be modified to include the new query? I need the existing code to still work.
            – kil lie
            Nov 26 '18 at 11:06


















          • Thank you for your response. Can my existing query be modified to include the new query? I need the existing code to still work.
            – kil lie
            Nov 26 '18 at 11:06
















          Thank you for your response. Can my existing query be modified to include the new query? I need the existing code to still work.
          – kil lie
          Nov 26 '18 at 11:06




          Thank you for your response. Can my existing query be modified to include the new query? I need the existing code to still work.
          – kil lie
          Nov 26 '18 at 11:06


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53448688%2fmultiple-meta-key-select-on-on-wordpress-database%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)