0

I use this SQL statement:

SELECT * FROM wp_posts
    LEFT JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
    LEFT JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
    WHERE wp_posts.post_type = 'post'
        AND wp_posts.post_status = 'publish'
        AND wp_posts.post_status != 'draft'
        AND wp_term_taxonomy.term_id = 17
        OR wp_term_taxonomy.term_id = 18
        OR wp_term_taxonomy.term_id = 19
    ORDER BY post_date_gmt DESC 
    LIMIT 6 
    OFFSET 6

but result of this show posts that have post_status = draft.

Why this happening?

1

Well, first of all, you use where post_status = 'publish'. So you wouldnt need to explicitly tell the drafts not to show up by using wp_posts.post_status != 'draft'

I think, your SQL says somthing like:

Grab all posts which are published and not drafted and attached to term_id 17 OR grab all posts which are attached to term_id 18 OR grab all posts which are attached to term_id 19.

So you should use some brackets to say something like

Grab all posts which are published and either attached to term_id 17 or term_id 18 or term_id 19

Try this:

SELECT 
    * 
FROM 
    wp_posts 
    LEFT JOIN 
    wp_term_relationships ON (
        wp_posts.ID = wp_term_relationships.object_id
    ) 
    LEFT JOIN 
    wp_term_taxonomy ON (
        wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    ) 
WHERE (
    wp_posts.post_type = 'post' AND 
    wp_posts.post_status='publish' AND 
    (
        wp_term_taxonomy.term_id = 17 OR wp_term_taxonomy.term_id = 18 OR wp_term_taxonomy.term_id = 19 
    )
)
ORDER BY 
    post_date_gmt DESC 
    LIMIT 6 
    OFFSET 6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.