I have tried adding a custom filter around my query, using:

$where .= ' AND ' . $wpdb->posts . '.post_excerpt IS NOT NULL AND ' . $wpdb->posts . '.post_content IS NOT NULL';

but that doesn't seem to work. The SQL looks correct (I'm amateur) but it doesn't show correct content and breaks the original query (it was filtering by a custom post type, but the above filter makes query display pages too).

The only way I can think is to query many more posts than I need, do a check on each one for excerpt / content, count through and then break out of loop but that doesn't feel correct.

(NB I will also want to strip shortcodes out of content when displaying).


  • NULL is considered as false. instead, use .post_excerpt<>'', which compares it to an empty string. – Jack Johansson Feb 22 '18 at 20:31

Below is an example — tested and worked.

The SQL query is:

AND ({$wpdb->posts}.post_content != '' OR {$wpdb->posts}.post_excerpt != '')

And the example:

// This would be in functions.php or somewhere else where appropriate.
function posts_where_require_content_or_excerpt( $where, $wp_query ) {
    global $wpdb;

    $where .= " AND (" .
        " {$wpdb->posts}.post_content != ''" .
        " OR {$wpdb->posts}.post_excerpt != ''" .

    return $where;


add_filter( 'posts_where', 'posts_where_require_content_or_excerpt', 10, 2 );
query_posts( array(
    'post_type'           => 'post',
    'posts_per_page'      => 5,
    'ignore_sticky_posts' => true,
) );
remove_filter( 'posts_where', 'posts_where_require_content_or_excerpt', 10, 2 );

if ( have_posts() ) {
    echo '<ul>';

        while ( have_posts() ) {

            echo '<li>';

                echo '<h2>'; the_title(); echo '</h2>';
                echo '<p>'; the_excerpt(); echo '</p>';

                printf( '<p><a href="%s">Read More &rarr;</a></p>',
                    esc_url( get_permalink() ) );

            echo '</li>';

    echo '</ul>';

    //the_posts_pagination(); // Needs extra work.

  • 1
    Thank you @sally, just about got this to work! (I had to change it to use new WP_Query, but it works well.) – Jake Feb 23 '18 at 12:03

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.