Wordpress: Wp_Query - find posts with meta_query OR tax_query

273 Views Asked by At

I want to find posts which have a specific post meta key/value or posts which are in a specific category. Here is the query. I want to combine the tax_query and the meta_query with OR, but AFAICS there's no way to do this.

$args = [
    'post_type'      => 'my-event',
    'post_status'    => 'publish',
    'posts_per_page' => -1, 
    'orderby'        => 'title', 
    'order'          => 'ASC',
    'cat'            => 'home',
    'tax_query'      => [
        [
            'taxonomy'         => 'event-cat',
            'terms'            => [
                'open',
            ],
            'field'            => 'slug',
            'operator'         => 'IN',
            'include_children' => true,
        ],
    ],
    'meta_query'     => [
        [
            'key'     => 'event_author',
            'value'   => [1,7,11,15],
            'compare' => 'IN',
        ],
    ],
];

$loop = new WP_Query($args);

Result should be: All posts (events) which are in the category 'open' (no matter who the author is) AND all posts which are from one of the specified authors (no matter in which category the event is).

On SO I found a few similar questions and I think I have to create a SQL query to find a solution but I don't know how to do it. Hope that someone can help me here. Thanks and best regards.

1

There are 1 best solutions below

0
On

In order to make custom,unconventional and a bit complicated queries like this, you have to learn some basic SQL Syntax. Trust me, SQL basics are not so hard to learn, and they are really worth it if you consider how many projects in many different programming languages need some SQL knowledge.

WordPress gives you the option to make custom queries with wpdb::get_results() function. In case you already know how to use SQL syntax, try exploring the wordpress database a little, and you will find what table columns you need to extract to get the result you want.