Querying for multiple post types in SQL

My site is using a plugin for displaying an archive widget. It only displays the 'post' post-type. I currently have three different post-types. Looking through the code I found:

$where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish' AND post_date = now()" );

It seemed like a easy fix. First I changed WHERE post_type = 'post' to WHERE post_type = 'privacy, security' but that didn't work.

Then I did WHERE post_type = array('privacy', 'security') AND post_status = 'publish' AND post_date = now()" ); but that also didn't work.'

Topic post-type-support post-type Wordpress

Category Web


Couple of things to note; one's a WordPress issue, the other an SQL issue:

For WordPress, instead of editing the plugin files directly, you should use the 'getarchives_where' filter it provides, and change the query there.

For SQL, in order to query for multiple post types, you need a conditional statement in the query, so looking for both 'privacy' and 'security' post types requires an OR can be done with an IN.

WHERE post_type IN ('privacy', 'security') 
    AND post_status = 'publish' 
    AND post_date <= now()

Combine this with the plugin's filter and you get:

apply_filters( 'getarchives_where', function( $where ) {
   return 'WHERE post_type IN ("privacy", "security") AND post_status = "publish" AND post_date <= now()';
    }, 10, 1 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.