Skip to content
Advertisement

How to JOIN postmeta table and get the posts before the date of custom field using SQL

I’d like to combine wp_posts table and wp_postmeta table and get the posts before the date of custom field.

Code with only wp_posts table is below and it works fine

I want to add “postmeta table” to “post table” and get only posts before the date of custom field

if it is “WP_Query”, meta query would be below

I tried code below but it returns empty array.

When it is added only meta_key, it returns the same result as “Code for only wp_posts table”(first code)

I’d like to know how to add conditions “key”, “value” and “compare”.

I’m very beginner on SQL and hope someone help me.

Advertisement

Answer

The changes I see between your original and new queries are the LEFT JOIN and the added conditions to your WHERE clause. Adding a LEFT JOIN to your initial query won’t result in an empty array unless one of these is true:

1) Your original query returned 0 rows
2) Your new query has additional filtering in the WHERE clause

If you want to return all your initial rows (i.e. from $wpdb->posts) and include any matching rows from $wpdb->postmeta, then just move the extra AND conditions from the WHERE clause to the JOIN:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement