WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I guess you're at a point where this kind of stuff so far isn't ready. Please show where you hooked in. – kaiser Mar 21 '12 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 Mar 21 '12 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 Mar 21 '12 at 8:13
@kaiser, there you go, Sir! Thanks! =) – jilseego Mar 21 '12 at 8:33
up vote 1 down vote accepted

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' );
share|improve this answer
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 Mar 21 '12 at 8:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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