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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:41:20+00:00 2026-06-18T12:41:20+00:00

I like to LEFT JOIN both tables, wp_posts and wp_postmeta. In my posts i

  • 0

I like to LEFT JOIN both tables, wp_posts and wp_postmeta. In my posts i have create several meta box options.

Lets say I have create a custom post type called “ads” and for each ad, i have the publication date and the expiration date as meta options.

the question is, how can I join the two tables, in order to get the ID of the post, from the wp_posts table where the current date is higher than the start date in the post meta table and lower than the expiration date in the post meta table.

for non WP users the wp_posts table is a traditional table, where each row represents a post and the wp_postmeta is a key value pare table where based on the post id, contains more than one row to describe the meta options of the post.

Is there a way to make that query in a single MySQL Query ?

kind regards.

  • 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-06-18T12:41:22+00:00Added an answer on June 18, 2026 at 12:41 pm

    I take you to mean that “start date” is the “publication_date”.

    Absent a schema definition, I will guess at the column names and datatypes

    One way to get this would be to use correlated subqueries in EXISTS predicates, though this may not be the most efficient approach:

    SELECT p.id
      FROM wp_posts p
     WHERE EXISTS
           ( SELECT 1
               FROM wp_postmeta m1
              WHERE m1.wp_posts_id = p.id
                AND m1.meta_option_name = 'publication date'
                AND m1.meta_option_date_value <= NOW()
           )
       AND EXISTS
           ( SELECT 1
               FROM wp_postmeta m2
              WHERE m2.wp_posts_id = p.id
                AND m2.meta_option_name = 'expiration date'
                AND m2.meta_option_date_value > NOW()
           )
    

    You specifically asked about using a LEFT JOIN

    An equivalent result can be obtained using LEFT JOIN operations (although this query is really equivalent to doing INNER JOIN operations):

    SELECT p.id
      FROM wp_posts p
      LEFT
      JOIN wp_postmeta m1
        ON m1.wp_posts_id = p.id 
       AND m1.meta_option_name = 'publication date'
       AND m1.meta_option_date_value <= NOW()
      LEFT 
      JOIN wp_postmeta m2
        ON m2.wp_posts_id = p.id
       AND m2.meta_option_name = 'expiration date'
       AND m2.meta_option_date_value > NOW()
     WHERE m1.id IS NOT NULL
       AND m2.id IS NOT NULL
     GROUP 
        BY p.id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, Foo and Bar, I'd like to join them together so
I'd like to do a left join using only certain rows of the first
My query string is like: SELECT ... FROM maintable LEFT JOIN table1 on (maintable.id
My SQL query looks like this... sekect * FROM mail_headers a LEFT JOIN mail_headers_body
I have some text that uses Unicode punctuation, like left double quote, right single
I'm trying to make JTextPane with two columns, both aligning to left like that:
I have a two column layout: http://jsfiddle.net/KqQ42/1/ Now I would like that the left
I currently have two tables, one is products and the other is options (colour,
I have two tables like this. Table1 Column | Type | ---------+------------------+ cod |
I have Table1 and Table2 in the form of IEnumerable<DataRow> . Both the tables

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.