1

I have two functions that do the same action: they add a title prefix for expired posts. Which of these functions will affect more the site performance?

function expired_facebook_events_title_prefix_( $title, $id = null ) {

    if( is_singular( 'facebook_events' ) && 'expired' == get_post_status() && in_the_loop() ) {
        return '[expirat] ' . $title;
    }

    return $title;
}
add_filter('the_title', 'expired_facebook_events_title_prefix_', 10, 2);


function expired_facebook_events_title_prefix( $title ) {
    global $post;
    if( $post->post_type == 'facebook_events' && $post->post_status == 'expired' && in_the_loop() ){
        return '[expirat] ' . $title;
   }
   return $title;
}
add_filter( 'the_title', 'expired_facebook_events_title_prefix' );
1
  • 2
    Why not use something like xdebug and find that out?
    – kero
    Commented Oct 28, 2017 at 14:07

1 Answer 1

3

There is no significant difference. Both will use a cached version of the current $post, in one you will access it via global $post, in other via functions that will get the data from the $post in the cache. I would suggest moving the in_the_loop() condition to be first in the IF.

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.