3

I have a plugin which adds the method to "the_content" filter.

add_filter('the_content', 'myFilteringFunction', 10000);

Within that function I want to add some links on the beginning and the end of the content. But I only need to do it for the "main" content of the displayed page so - not in the any of the widgets, not in the footer, header etc.

Moreover I only want it to be included for the custom post type which I defined in the same plugin. So I figured out that kind of check, thinking it would be enough.

if( is_single() && get_query_var('post_type') == 'myCustomPostType' && is_main_query() )

Unfortunately it's not working as intended - at least not in every case.

On the page the plugin WP Types is installed, it's not working (the links are added despite the condition). Why?

2
  • Show us the function and tell us which template this is. So far it's a guessing game :)
    – kaiser
    Feb 13, 2014 at 13:36
  • @kaiser It's not about the function I deliberately didn't put much code in here not to blurr the problem. The question is not about the functionality but rather how to detect in which part of the page "the_content" filter has been called.
    – MythThrazz
    Feb 13, 2014 at 14:02

2 Answers 2

3

According to Wordpress documentation, you can accomplish this using this code:

add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );

function filter_the_content_in_the_main_loop( $content ) {

    // Check if we're inside the main loop in a single post page.
    if ( is_single() && in_the_loop() && is_main_query() ) {
        return $content . "I'm filtering the content inside the main loop";
    }

    return $content;
}

Or use the same logic via a single line at the beginning of the filter callback function:

if( !is_single() || !in_the_loop() || !is_main_query() ) return $content;
1
  • This is more reliable than the accepted answer due to is_main_query() usage.
    – RafaSashi
    Jan 19, 2023 at 12:22
3

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()

3
  • 1
    Thanks, that's what I was looking for - basically it's the same as getting the global $post and checking it's post_type field.
    – MythThrazz
    Feb 13, 2014 at 14:11
  • Yes, I found these to be a bit confusing at first. Glad it helped!
    – iyrin
    Feb 13, 2014 at 14:13
  • This helps target content based on post type, but will not be strictly limited to apply to main displayed content. Dec 12, 2017 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.