Support » Plugin: Relevanssi - A Better Search » Still redefining the global $post object…

Viewing 7 replies - 1 through 7 (of 7 total)
  • Actually that only does it if its an array of posts but you do this:

    $post = get_post($post->ID);

    Which means you reload the post from the database – thus wiping out the current in memory post structure.

    Any feedback on this?

    Plugin Author Mikko Saari


    Yes, it is done on purpose. The reason why is actually explained in the comments in the source code, you could’ve just read it there.

    If Relevanssi would just index the global $post object, Relevanssi would in some situations end up indexing a wrong revision of the post. By fetching the current version of the post, Relevanssi is guaranteed an up-to-date version.

    As you may notice, the global $post object is stored and restored after Relevanssi is done indexing the post.

    Is there a problem with this?

    So why use the global post object – why not just create your own copy rather than backing it up and then replacing it. If an object is a global object you really shouldn’t be manipulating it in such a way.

    For some reason on a install using your plugin the $post->ID suddenly gets unset and of course having had problems with your plugin over a similar problem a year ago I immediately looked at your code.

    Plugin Author Mikko Saari


    There are shortcodes, for example, which need to have the correct post in global $post in order to return appropriate results. Changing the code so that global $post is not touched will break those shortcodes. I got a nice bunch of error messages, too, when I tried it.

    If there is a case where Relevanssi doesn’t restore global $post object as it was, I’d like to know more. Perhaps there’s an oversight in the code that can be fixed?

    I still don’t understand why you think its OK to change the global object.

    I have situations where $post->ID becomes a null value and the only common factor is Relevanssi being installed.

    So I have to conclude that Relevanssi is nullifying part of the $post structure.

    Plugin Author Mikko Saari


    Because other plugins that use shortcodes expect the global $post to be changed. If I don’t change, it will break compatibility with several shortcode plugins. I simply don’t know any other way to do this so that those shortcode plugins work with Relevanssi.

    If all those plugins didn’t rely on global $post, then it’d be different story, but that’s the reality Relevanssi has to exist in.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Still redefining the global $post object…’ is closed to new replies.