0

I am trying to query info from a custom table created by a calendar feed. The calendar feed created a post with the title for each event, but the event data is in a different table instead of post_meta. Here is what I am trying with no results:

$sql = "SELECT post_id".
"FROM {$wpdb->prefix}calp_events".
"JOIN {$wpdb->prefix}posts ON (ID = post_id)";

$results = $wpdb->get_results( $sql );

The id of each calp_events is the same as the post ID. I want to be able to echo results from both tables after my query.

1

I think you need to distinguish which table your ID and post_id come from. Take a look below and notice how the tables get a variable assigned. You could also just use the table name, but this reads easier.

SELECT P.post_id FROM [prefix]_calp_events as EV
JOIN [prefix]_posts as P ON EV.ID = P.post_id 

You stated that you want to read the results from both tables. The join actually returns only matching records in both tables. Check out https://www.w3schools.com/sql/sql_join.asp for details on how joins work.

| improve this answer | |
  • Your answer is correct when it comes to the join function. I ended up having to go another direction to get the results I need. Thanks! – RiotAct Mar 29 '17 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.