I have two tables word_term_relationships and word_posts
What I’m doing is using a while loop in order to fetch a certain record from the word_term_relationships table where a certain value is true.
$query = "SELECT object_id
FROM `word_term_relationships`
WHERE `term_taxonomy_id` = '54'";
I then use another query within the loop to use the data that was retrieved from the previous query in order to fetch data from the other table word_posts
$query2 = "SELECT post_title, post_date, post_date_gmt, guid
FROM `word_posts`
WHERE `ID` = '$post_id'";
This I can do and works fine.
The only issue is that I then need to order the results by date and time, I can do this without the while loop and using the ORDER BY function and the word_posts table.
However, I’ve tried to link the tables within the query like this (below) in order to order the data. But obviously it isn’t correct – I just can’t pinpoint within the query what is wrong.
$query = "SELECT word_term_relationships.object_id
FROM word_term_relationships
WHERE word_term_relationships.term_taxonomy_id = '54'
ORDER BY word_posts.post_date ASC";
I know the above query is missing something, I was thinking a second where after the ORDER BY word_posts.post_date ASC.
A simple
INNER JOINwill solve your problem. Try this,