• Resolved Kramer

    (@kramer)


    Hi,

    I am writing filter plugin that does something with posts and it need to add CSS to page depending on posts contens. It may add diffrent CSS values into Head or may not – depending on posts contents.

    How can I get lists of posts on page from wp_head hook, or should use some other hook?

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Kramer

    (@kramer)

    To rephrase the question: I want to be able to enable hooks optionally depending on posts contents. I suppose, for that I need to access all posts on current page before wp_head hook is called.
    1. What is the WordPress startup sequence?
    When are thirdparty plugins loaded – before or after request processing and loading data drom DB?
    2. which function can be used to get array of loaded posts for current page?
    3. If posts are loaded after thirdparty plugins- will I have to load posts from inside plugin? This will result in loading same data twice which is unacceptable.

    I hope WordPress have solution for this.

    1. Before.
    2. Use the global variable $wp_query, and its property $wp_query->posts
    3. No. Use hooks: put all the code that needs to do stuff with the posts inside a function, and then hook that on to the_posts. Beware: the_posts is a filter, not a hook, so you’ll need to return the posts once you’re done with them. (see the Codex if you’re not sure what I mean).

    If you only want to run the function based on some condition, check that condition inside the function, and if it’s not true, return.

    Thread Starter Kramer

    (@kramer)

    Thanks for reply!

    I am trying $wp_query->posts, but this variable is NULL. I tried to call it form my hook and just from included plugin.

    What I need is to access posts from wp_head hooked function.

    There’s another possibility – to use get_posts(), but it ned a current page query parameters, if only I knew how to find that out the correct way, without parsing get/post vars.

    Or maybe there is a better way? Some global variable that has all posts for current request?

    $wp_query->posts is correct, make sure that you’re declaring $wp_query as a global variable. It won’t be available when the plugin file is parsed, the earliest hook/filter that it’s available for is the_posts, but wp_head should be fine.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to get posts from wp_head hook or similar?’ is closed to new replies.