WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
Still redefining the global $post object... (8 posts)

  1. Steve
    Member
    Posted 1 year ago #

    function relevanssi_index_doc has $post defined in its global variable list and then you still (a year after this was pointed out to you) redefine the $post object:

    if (is_array($post)) {
    	$post = get_post($post['ID']);
    }

    Why are you doing this?

    http://wordpress.org/extend/plugins/relevanssi/

  2. Steve
    Member
    Posted 1 year ago #

    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.

  3. Steve
    Member
    Posted 1 year ago #

    Any feedback on this?

  4. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    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?

  5. Steve
    Member
    Posted 1 year ago #

    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.

  6. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    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?

  7. Steve
    Member
    Posted 1 year ago #

    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.

  8. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic