Only retrieve posts where post_excerpt has been filled out

I’m trying to use get_posts to only return posts that have an excerpt. Done lots of searching and have been trying to use “posts_where” filter on the query but my SQL is lacking. This is what I’m using which in theory I think should work, but really I have no idea on the sql and can’t work out how to print the sql string for this query to debug…

$args = array(
    'post_type' => 'testimonial',
    'numberposts'     => 1,
    'orderby'         => 'rand',
);

add_filter( 'posts_where' , 'posts_where_excerpt_not_empty' );

$post = get_posts($args);

remove_filter( 'posts_where' , 'posts_where_excerpt_not_empty' );

[...]

function posts_where_excerpt_not_empty( $where ) {
    $where .= " post_excerpt NOT NULL";
    return $where;
}

Related posts

Leave a Reply

2 comments

  1. The post_excerpt column is a string and is not filterable using “IS NOT NULL” . To query for an empty string you can use the != operator.

    function posts_where_excerpt_not_empty( $where ) {
        $where .= " AND post_excerpt != '' ";
        return $where;
    }
    

    Also get_posts suppresses filters by default so you will need to call it with suppress filters set to false or use another query method.

    $posts = get_posts( array( 'suppress_filters' => FALSE ) );
    
  2. i Might be mistaken but wont a “check if empty” condition do the trick?

    <?php 
    query_posts(array( 
    'post_type' => 'testimonial',
    'showposts' => 1,
    'orderby' => 'rand'
    ) );  
    
    $checExcerpt = get_the_excerpt();
    if !($checExcerpt = '') {
    // Put the echo commands here
    } else {
    echo 'Sorry You forgot to enter Descriptions.. search engine love does!';
    } ?>
    

    .
    I think this should be inside the loop in the page you want to display the content retured from the check..