Skip to content
Advertisement

Add OR in WHERE

I have SQL query:

SELECT
    p.ID,
    p.post_author,
    p.post_status,
    p.post_name,
    p.post_parent,
    p.post_type,
    p.post_date,
    p.post_date_gmt,
    p.post_modified,
    p.post_modified_gmt,
    p.comment_count
FROM
    {$wpdb->posts} p
WHERE
    p.post_password = ''
    AND p.post_type = '%s'
    AND p.post_status = 'publish'
    AND YEAR(p.post_date_gmt) = %d
    AND MONTH(p.post_date_gmt) = %d
    {$exPostSQL}
    {$exCatSQL}
ORDER BY
    p.post_date_gmt DESC

And I want to adjust it so it looks for p.post_status = 'publish' OR p.post_status = 't_active'.

I tried:

AND p.post_status = 'publish' OR p.post_status = 't_active'

But it didn’t work. How do you achieve this?

This is an entry level question – SQL is not my jazz.

Advertisement

Answer

You need to use brackets to make clear what condition you’re doing the OR on:

AND (p.post_status = 'publish' OR p.post_status = 't_active')

Alternatively you can use an IN clause:

AND p.post_status IN ('publish', 't_active')
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement