0

I am Trying to add some content before the posts container I use this code

add_action( 'wp', 'mainFunc' );
function mainFunc( $query ) {
    if ( is_home() && $query->is_main_query()) {
        add_action("loop_start","anotherFunc");
    }
}

and I have anotherFunc() too.

but I get

Fatal error: Call to undefined method WP::is_main_query() in C:\xampp\htdocs\wp\wp-content\plugins\topMessage\topmessage.php on line 40

Any Ideas ?

1

Wrong hook, because what gets passed to wp is the

Current WordPress environment instance

not

The WP_Query instance

like it is e.g. passed to loop_start.

You could actually just do your check inside the function callback you hook to loop_start.

Edit/note:

You could just have done global $wp_query; inside the wp hook, it is actually the first one where access to $wp_query is possible, to have access to it - not that I recommend doing it though. A better place - or at least the one I probably would go for - to hook into is the pre_get_posts action, which does get passed $query - the WP_Query object - by reference.

| improve this answer | |
  • " You could actually just do your check inside the function callback you hook to loop_start. " Can you Explain More ? – Zero Burnner Jul 22 '15 at 13:09
  • Put if ( is_home() && $query->is_main_query() ) {//code} check in »anotherFunc()« instead. @ZeroBurnner – Nicolai Jul 22 '15 at 13:11
  • What a professional Idea .... ty Solved ... – Zero Burnner Jul 22 '15 at 13:17

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.