The exact error is:

Call to undefined function get_userdata() in /wp-includes/query.php on line 3567

I am using get_posts() in my plugin, which in turn uses setup_postdata() wherein get_userdata() is executed. I have no clue what's going on. I looked at query.php and it looks like get_userdata() is a global function, it shouldn't have any problem accessing it right?


My plugin is in a class form. This is just the basic structure. There are multiple functions in the actual code actually. Please take note of the get_posts() function where the post query occurs. If I remove it, the error won't show up.

class myPlugin() {
   var $post_type = '';

   function __construct($post_vars = array()){
       if ($post_vars) $this->post_type = $post_vars['post_type']

   function get_posts() {
       $args = array('numberposts' => -1, 'post_type' = $this->post->type, 'post_status' => 'publish');
       $myposts = get_posts( $args );
       foreach( $myposts as $post ) : setup_postdata($post);

return new myPlugin($_POST);

If I place the query in functions.php(and maybe outside the class or function scope which I have not tried), it works. That's my workaround for now, but I'd like to integrate it into my plugin.

  • I guess you're at a point where this kind of stuff so far isn't ready. Please show where you hooked in.
    – kaiser
    Commented Mar 21, 2012 at 7:54
  • @kaiser well I guess you didn't answer my question. In fact that's the only certain thing I can gather in this page so far.
    – jilseego
    Commented Mar 21, 2012 at 8:10
  • I didn't answer your question. I commented and asked for more detail (update your Q): A) Where do you have this code? B) Where do you hook in?
    – kaiser
    Commented Mar 21, 2012 at 8:13

1 Answer 1


Your problem is that you're adding a return new CLASSNAME( $data ); at the end of a file(?).

You need to make that call inside a function and add the fn callback to a hook like this:

function wpse46288_call_the_funk()
    return new CLASSNAME( $data );
add_action( 'init', 'wpse46288_call_the_funk' );
  • Yup that's the last line in the file. I see, if this will work, I'll send you a beer. =) If you don't mind, could you elaborate your answer?
    – jilseego
    Commented Mar 21, 2012 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.