I'm dealing with a weird case using WP_Query and I'm not sure if it's possible to solve with it. I'd like to find the equivalent of SQL UNION ALL. For example: If I run something like this :point_down: I'll have the expected result
$wpdb->get_results("(
SELECT ID
FROM wp_posts
WHERE post_type = 'my_taxonomy'
)
UNION ALL
(
SELECT ID
FROM wp_posts
WHERE post_type = 'page'
AND post_parent = " . post_parent_id ."
) ORDER BY ID DESC", OBJECT);
And then if I try to simulate the same behaviour with WP_Query I get
Combination of 2 queries:
Query #1
new WP_Query([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent__in' => post_parent_id
]);
AND
Query #2
new WP_Query([
'tax_query' => array(
array(
'taxonomy' => 'my-taxonomy',
'field' => 'slug',
'terms' => 'my-taxonomy',
),
),
]);
Finally, I've tried to include both in a single WP_Query
but I have conflict with post_parent__in
since it only applies to page and since my_taxonomy does not have post_parent__in
then the condition is not working. I only get pages.
new WP_Query([
'post_status' => 'publish',
'post_type' => array('page', 'my_taxonomy'),
'post_parent__in' => [$post_parent_id],
]);
Any idea? Thanks in advance
I'm dealing with a weird case using WP_Query and I'm not sure if it's possible to solve with it. I'd like to find the equivalent of SQL UNION ALL. For example: If I run something like this :point_down: I'll have the expected result
$wpdb->get_results("(
SELECT ID
FROM wp_posts
WHERE post_type = 'my_taxonomy'
)
UNION ALL
(
SELECT ID
FROM wp_posts
WHERE post_type = 'page'
AND post_parent = " . post_parent_id ."
) ORDER BY ID DESC", OBJECT);
And then if I try to simulate the same behaviour with WP_Query I get
Combination of 2 queries:
Query #1
new WP_Query([
'post_type' => 'page',
'post_status' => 'publish',
'post_parent__in' => post_parent_id
]);
AND
Query #2
new WP_Query([
'tax_query' => array(
array(
'taxonomy' => 'my-taxonomy',
'field' => 'slug',
'terms' => 'my-taxonomy',
),
),
]);
Finally, I've tried to include both in a single WP_Query
but I have conflict with post_parent__in
since it only applies to page and since my_taxonomy does not have post_parent__in
then the condition is not working. I only get pages.
new WP_Query([
'post_status' => 'publish',
'post_type' => array('page', 'my_taxonomy'),
'post_parent__in' => [$post_parent_id],
]);
Any idea? Thanks in advance
Share Improve this question asked Feb 7 at 9:52 DantoDanto 1 New contributor Danto is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0You can run 2 wp_query, get the ids and pass those as the post__in
parameter to a 3rd wp_query. Not efficient but will do the job.
Or you can use a subquery and do it all with just one wp_query. Something like the following:
add_filter( 'posts_where', 'myprefix_posts_where', 10, 2 );
new WP_Query([
'post_status' => 'publish',
'post_type' => array('page', 'my_taxonomy'),
/* do not add taxonomy or parent parameters here */
]);
...
...
remove_filter( 'posts_where', 'myprefix_posts_where', 10, 2 );
function myprefix_posts_where( $where, $query ) {
global $wpdb;
$where .= " AND {$wpdb->posts}.ID IN (
(
SELECT ID
FROM {$wpdb->posts}
WHERE post_type = 'my_taxonomy'
)
UNION ALL
(
SELECT ID
FROM {$wpdb->posts}
WHERE post_type = 'page'
AND post_parent = " . post_parent_id ."
)
)";
return $where;
}
Maybe there is a better way to do this but I can't think of any.