get_posts() seemingly ignoring post_type

I built a workaround for now, but would really love to know what is causing this. The workaround was obtained by removing changing “suppress_filters” to true and then adding a function that adds to the where clause of the SQL…

But I would rather know how to fix this at the root rather than a bandaid

My workaround was accomplished by adding the following to my functions.php:

function leads_post_type_where( $where ) {
$clause = "";
$post_types=get_post_types($args,'names');
foreach ($post_types as $post_type ) {
    if (trim($post_type)!="" && trim($post_type) != "ditl_post") {
        if ($clause==""){
            $clause .= "'$post_type'";
        } else {
            $clause .= ", '$post_type'";
        }
    }
}   

$where .= ' AND post_type NOT IN ('.$clause.')';
return $where;
}

Then updated my get_posts call to:

$args = array(
      'numberposts' => 5,
      'order' => 'DESC',
      'orderby' => 'date',
      'post_type' => 'leads',
      'post_status' => 'publish',
      'post__not_in' => array($post->ID),
      'suppress_filters' => false
  );
    add_filter( 'posts_where', 'leads_post_type_where' );
    $result = get_posts($args);
    remove_filter( 'posts_where', 'leads_post_type_where' );

Leave a Comment