1

I am using global $post multiple times in a class. These are my doubts

  1. Is that a bad practice?
  2. Can it result in slow websites?
  3. What's the proper way to use it inside a class

    class Test{
        public function(){
            global $post;
        }
    
        public function2(){
            global $post;
        }
    
        public function3(){
            global $post;
        }
    }
    
  • The issue I do see is that you're missing the function keyword: eg, public function() should be public function function_name(). – Pat J Aug 3 at 23:18
  • 1
    @PatJ sorry that was a typo I just wanted to show to the structure – Latheesh V M Villa Aug 3 at 23:24
  • The correct answer depends entirely on how you're using it. The vast majority of the time there's better ways than using the global $post. Your question is not specific enough to answer though. – Jacob Peattie Aug 4 at 6:04
1

There's nothing wrong with using the global $post variable per se, although there are people who will say that using global variables is always bad. But since you're using it multiple times in the same object, it would be better to just get the post once and store it in a class property.

I prefer using the WordPress get_post() function because it looks cleaner and get_post() does some stuff if the global $post variable isn't a WP_Post object. Using the global $post variable multiple times in a class will not slow down your site though, if that's your main worry.

So my class might look something like this:

class Test {
  protected $post;
  public function __construct() {
    $this->post = \get_post();
  }
  public function fizzbuzz() {
    //* Use $this->post instead of global $post
  }
  ... and etc. Mainly etc.
}

If you're looking to modify the global $post object, a better method would be to use the the_post action hook to access the $post object immediately after it is setup.

class Test {
  public function the_post( $post_object ) {
    //* Do something useful with the post object
  }
}
add_action( 'the_post', [ new Test(), 'the_post' ] );
  • Thank you all clear :) – Latheesh V M Villa Aug 3 at 23:24
  • 3
    Given the nature of how the global post variable changes, it doesn’t seem logical to me to store it once. If you do that you’re storing a reference to whatever the global post variable was when the class is instantiated, which is not necessarily going to be the same post that’s referenced inside a hook, especially if the hook runs for multiple posts. For example, this would break your code if one of the class methods was a filter on the_title. – Jacob Peattie Aug 4 at 1:06
  • 1
    I can't actually think of a use case for the OPs code except using the class as a pseudo-namespace. The fact that they want/need to use the global post object in 3 different methods suggests a problem with the class architecture. – Nathan Johnson Aug 4 at 22:32

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.