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.
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.
Share Improve this question asked May 8, 2019 at 0:12 Galloway GrumblefieldGalloway Grumblefield 351 silver badge8 bronze badges 5 |1 Answer
Reset to default 1You can do this using the WP APIs like this:
$args = [
'post_type' => 'post',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'your_taxonomy_name',
'field' => 'slug',
'terms' => array( 'arizona', 'speech-language-pathology' ),
]
],
];
$query = new WP_Query( $args );
Then you can use $query
as you would any standard post loop.
If you only want the IDs you can use the fields
option of WP_Query
, but keep in mind you won't gain all the caching and performance benefits you might have gotten otherwise, especially if you're going to pass the ID into get_post
$args = [
'post_type' => 'post',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'your_taxonomy_name',
'field' => 'slug',
'terms' => array( 'arizona', 'speech-language-pathology' ),
]
],
'fields' => 'ids'
];
$query = new WP_Query( $args );
$posts = $query->get_posts(); // $posts is an array of post IDs
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
. – MikeNGarrett Commented May 8, 2019 at 0:23WP_Query
? – Tom J Nowell ♦ Commented May 8, 2019 at 0:25$wpdb->get_col( $wpdb->prepare($query, $parentID, $filterValue ));
– Galloway Grumblefield Commented May 8, 2019 at 0:28get_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 – Tom J Nowell ♦ Commented May 8, 2019 at 13:06$out = call_user_func_array('array_intersect',$result);
– Galloway Grumblefield Commented May 8, 2019 at 13:29