I realize this isn't the most elegant sql. I modified this query where the original query only selected from a single category (the slug
field of the terms
table). I am trying to obtain a result set which returns items having multiple categories (or slugs) as properties.
This does not return rows, even though the items do have both 'arizona' and 'speech-language-pathologist' in their categories list:
SELECT p.ID FROM wp_term_relationships tr
JOIN wp_posts p ON tr.object_id = p.ID
JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms t ON tt.term_id = t.term_id
WHERE p.post_parent = 220 AND t.slug = 'arizona' AND t.slug = 'speech-language-pathology' AND p.post_status = 'publish'
However, if I remove one of the AND conditions, this query does produce a result set:
SELECT p.ID FROM wp_term_relationships tr
JOIN wp_posts p ON tr.object_id = p.ID
JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms t ON tt.term_id = t.term_id
WHERE p.post_parent = 220 AND t.slug = 'arizona' AND p.post_status = 'publish'
My SQL is a little rusty, I don't see how the t.slug='something' AND t.slug='something-else' is illogical.
WP_Query
to retrieve the post you're looking for, ie the post that the child of220
, has statuspublish
, and is tagged witharizona
andspeech-language-pathology
.WP_Query
?$wpdb->get_col( $wpdb->prepare($query, $parentID, $filterValue ));
get_col
? Are you trying to get the IDs of all posts in arizona with a particular post parent? If so why not useWP_Query
? It can do that, and can even be faster than a direct query in a lot of cases. I'm still unclear what problem this solves$out = call_user_func_array('array_intersect',$result);