• Resolved Adam

    (@umbercode)


    I have posts and pages that have a meta-value set to a certain value.
    Whenever such a post or page is encountered I need to redirect to another page (I am doing this via a plugin). This should also work if someone runs a WP_Query on a template-file directly.
    So I need a hook after a WP_Query has run, but before the content is rendered. Which one(s) can I use for that?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The action ‘loop_start’ fires before each post is output, but a redirect at this point will fail because page content has already been sent.

    The filter ‘the_posts’ fires almost immediately after the query returns, but you’d have to sift through all the posts looking for a value. There’s a better chance of a redirect working here though, it depends on the template really, some send content before a query is even run, so a redirect will never work.

    Thread Starter Adam

    (@umbercode)

    I’ll try both of them, thanks didn’t know about the ‘loop_starts’ one. What about ‘wp’? As I understand it it will also fire before output, but I suspect it will not take custom WP_Queries into account?

    Moderator bcworkz

    (@bcworkz)

    Right, ‘wp’ action is much too early, there is no sign of any query being formed at that point. The only thing useful for you there is it’s also before any page output, so a redirect would surely work, except there is no condition to check for, so the redirect would happen every time 🙁

    Thread Starter Adam

    (@umbercode)

    Ok, thanks again. However I am abandoning this line of thought since it will become near impossible to cater for every possible post/page query like this. So I am going to check the meta-key on the filters ‘the_content’ and ‘the_excerpt’ instead and use a javascript redirect or a content-change. I just hope it will not slow down the site too much.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Check meta-value just before rendering’ is closed to new replies.