What does the $posts_join filter join to?
I am trying to use the posts_join
filter to join 2 tables: wp_postmeta
to wp_posts
. I am a novice at SQL, so I'm not sure if I am confused because I don't understand how WordPress is implementing posts_join
, or if I just don't understand the SQL syntax.
My questions are:
- Why is only one table used in the join statement? I thought 2 tables were needed for a join statement
- Is this because the
posts_join
filter assumes you are always joining a table towp_posts
?
The example in the Codex at https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
uses a similar situation of joining wp_postmeta
to wp_posts
.
$join .= "LEFT JOIN $wpdb-postmeta ON $wpdb-posts.ID = $wpdb-postmeta.post_id ";
Based on this example, I can see that $wpdb-postmeta
(i.e. wp_postmeta
) is explicitly stated as one of the tables to join. But from my understanding of SQL, I thought $wpdb-posts
would also have to be stated BEFORE the ON clause, like so:
$join .= "$wpdb-posts LEFT JOIN $wpdb-postmeta ON $wpdb-posts.ID = $wpdb-postmeta.post_id ";
So I am very confused why $wpdb-posts
is not specified before the ON clause. How else does WordPress know what is the second table to be joined?
Thank you for your help.
Topic join-tables Wordpress sql
Category Web