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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:58:43+00:00 2026-05-24T20:58:43+00:00

I recently posted a question about connecting two queries to omit elements found in

  • 0

I recently posted a question about connecting two queries to omit elements found in a second:

MYSQL – How to join two queries to omit elements found in the second query (or perhaps any better solution?)

Now I am wondering how to look at a number of meta_keys and their corresponding values to achieve something else.

I am essentially building an event system with custom fields, simplified, an event will have:

  • Start Date
  • End Date
  • Ongoing (Yes or No)

These are stored as

-----------
wp_postmeta
-----------
meta_key
meta_value
post_id

e.g.

-----------
wp_postmeta
-----------
Start Date
2011-07-30
10

-----------
wp_postmeta
-----------
End Date
2011-08-30
10

-----------
wp_postmeta
-----------
Ongoing
Yes
10

My confusion lies in trying to look at multiple wp_postmeta entries and connecting them with the appropriate post.

For example, I want to find the posts that:

  • Start Date > Today
  • End Date > Today
  • Ongoing = Yes

Building upon the previous question, I am trying this query:

SELECT * FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id

AND wp_posts.ID IN (
SELECT post_id FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'Start Date'
AND wp_postmeta.meta_value > NOW())

AND wp_posts.ID IN (
SELECT post_id FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'Ongoing'
AND wp_postmeta.meta_value = 'Yes')

AND wp_posts.ID IN (
SELECT post_id FROM wp_postmeta
WHERE wp_postmeta.meta_key = 'End Date'
AND wp_postmeta.meta_value > NOW())

Which is not really working.

I feel like there must be a way to find all the related meta_keys and join their values to the post table, so it can be more easily accessed. For example, is it possible to find the meta_value for ‘Start Date’ and join it to the wp_posts with a column title start_date_value or something like that?

Or how should I be approaching this problem instead?


Using the answer from @karevn, I came up with the following code which works exactly as I want:

$query = array(
        'category_name' => 'event',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'Start Date',
                'value' => $today,
                'compare' => '>'
            ),
            array(
                'key' => 'End Date',
                'value' => $today,
                'compare' => '>'
            ),
            array(
                'key' => 'Ongoing',
                'value' => 'Yes',
                'compare' => '='
            ),
        )
    );

I think this is a pretty powerful feature of wordpress.

  • 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-24T20:58:43+00:00Added an answer on May 24, 2026 at 8:58 pm

    You can use WP_Query class with meta_query option and avoid writing your own SQL at all. See Codex: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters. It is supposed to be a better way in WordPress. Your query will look pretty much like this then:

    $query = new WP_Query(array('meta_compare' => array(
        array('key' => 'Start Date', 'compare' => '>', 'type' => 'DATE', 'value' => $today),
        array('key' => 'End Date', 'compare' => '>', 'type' => 'DATE', 'value' = $today),
        array('key' => 'Ongoing', 'value' => 'Yes')));
    

    The side effect of using this code is that its results may be cached by DB caching plugin.

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

Sidebar

Related Questions

I recently posted a question about Azure... is it really an OS? I understand
I recently posted a question about centering a page with CSS. I figured out
I recently posted a question about getting last 3 results in table in the
I recently posted a question about optimizing the algorithm to compute the Levenshtein Distance,
Recently i posted a question about what the variable is of a file which
Recently I posted a question about the html helper dropdownlist and got it working
I've recently posted a general question about RAII at SO . However, I still
I recently posted a question about a WCF Restful web service that I am
I recently posted a question on stackoverflow about the less popular js function document.captureEvents()
I recently posted this question about codes for a gift-card-like voucher that users can

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.