Fast background: I have inherited a WP site that someone else codified and I am adding additional functionality.
I have made a plugin that creates a custom table. My query method is like this:
get_locations () {function
$ query = "SELECT
latitude
elongation
t1.next_port_name,
t2.post_title as rig_name,
t2.ID as post_id
FROM $ this-> vessel_table t1
INNER JOIN $ this-> posts_table t2
ON t1.post_id = t2.ID
JOIN LEFT $ this-> vessel_table t3
ON t1.post_id = t3.post_id
And t1.update_time < t3.update_time
WHERE t3.post_id IS NULL";
$vessel_pos = $this->wpdb-> get_results ($ query);
returns $ vessel_pos;
}
Now I am writing a new method, which instead of selecting all the containers, will only select the containers that are of a certain type. The boat type is a personalized taxonomy, so I am trying to use a fairly standard combination between the terms / terms taxonomy / terms relationships.
I got the taxonomy part working without consulting my personalized table, like this:
$ query = "SELECT
t2.post_title as rig_name,
t2.ID as post_id
FROM $ this-> posts_table t2
INNER JOIN $ this-> term_rel_table wtr ON (t2.ID = wtr.object_id)
INNER JOIN $ this-> term_tax_table wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
INNER JOIN $ this-> term_table wt ON (wt.term_id = wtt.term_id)
AND wtt.taxonomy = & # 39; rig_type & # 39; AND wt.term_id = $ rig_type ";
But I can not figure out how to integrate these two queries, so I will only return the expected results, with all the required data. This is my best effort so far:
search_locations ($ rig_type) {function
$ query = "SELECT
latitude
elongation
t1.next_port_name,
t2.post_title as rig_name,
t2.ID as post_id
FROM $ this-> vessel_table t1
INNER JOIN $ this-> posts_table t2
ON t1.post_id = t2.ID
INNER JOIN $ this-> term_rel_table wtr ON (t2.ID = wtr.object_id)
INNER JOIN $ this-> term_tax_table wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
INNER JOIN $ this-> term_table wt ON (wt.term_id = wtt.term_id)
JOIN LEFT $ this-> vessel_table t3
ON t1.post_id = t3.post_id
And t1.update_time < t3.update_time
WHERE t3.post_id IS NULL
AND wtt.taxonomy = 'rig_type'
AND wt.term_id = $rig_type";
$vessel_pos = $this->wpdb-> get_results ($ query);
returns $ vessel_pos;
}
This does not produce any error, but it does not return any results either. I'm really new to JOIN
so I assumed that this was mainly due to the lack of understanding of the concept.
So I've been reading about unions and I feel like I'm doing pretty well on the subject. But I can not take that last leap of logic to find out what I should do here, any suggestions?
Note: Mi $ this-> table_name
in the methods are, of course, the equivalent of $ wpdb-> table_name
.