最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - WP_Query equivalent of SQL UNION ALL

programmeradmin1浏览0评论

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.
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

发布评论

评论列表(0)

  1. 暂无评论