How to ensure “the_content” filter runs only for the main displayed content?

Try adding a condition to your filtering function that checks your post type against get_post_type.

if ( 'book' == get_post_type() )

If you wish to apply this filter to pages as well, try is_singular() and include your custom post type(s) as an argument.

is_singular('book');

This will return true if any of the following conditions are true:
is_single()
is_page()
is_attachment()
‘book’ == get_post_type()

Leave a Comment