Query posts only with actual text content (not including shortcode or images)
Here are some notes:
-
There’s a problem with how
$args
is defined:$args = new WP_Query(array( ... ) ); $the_query = new WP_Query( $args );
i.e. there are two
WP_Query
instances here but$args
must be an array. -
It looks like this part is problematic too:
$content = str_word_count( strip_tags( $content ) ); if( '' !== $content )
Note that
str_word_count()
returns a number or an array for other format input parameters.Here you’re using a strict empty string comparison.
-
You should use
pre_get_posts
action if possible, instead of introducing another secondary query. -
I can see another possible problem with this approach: If the query returns only posts that contains shortcodes or images, then there’s nothing to display.
-
Another approach might be to automatically store that information (e.g. as post meta) when you save the post.
If you store it under the post meta key
_wpse_searchable
then you could adjust your meta query:'meta_query' => [ 'relation' => 'AND', [ 'key' => '_thumbnail_id', 'compare' => 'EXISTS', ], [ 'key' => '_wpse_searchable', 'value' => true ] ],
If a post isn’t “searchable”, next time we update it, we could also just delete that custom field automatically.