0

I'm getting an:

 Fatal error: Call to undefined function is_user_logged_in() in C:\wamp\www\benracicot\wp-includes\query.php on line 2521

If I remove the $args from WP_Query I get no errors and I can print the return object (all empty values) What am I doing wrong in my plugin query code?

function getPostIDsByMonth($month){
    $args = array( 'monthnum' => 1, 'post_type' => 'post' );
    $ids = new WP_Query($args);
    return $ids;
}

$months = 1;
$bymonth = getPostIDsByMonth($months);
print_r($bymonth);
echo $bymonth->post_count;
if (! empty($bymonth)){

     while($bymonth->have_posts()): $bymonth->the_post(); 
        ?><h3><a href="#"><?php the_title(); ?></a></h3><?php 
        the_content();
     endwhile;

}
6
  • 1
    when are you trying to do this?
    – Milo
    Sep 27, 2013 at 21:59
  • I haven't hooked it to anything. Should I be? Sep 27, 2013 at 22:11
  • It doesn't necessarily have to be hooked but we still need to know where the code runs. My guess, though, is that it isn't hooked and should be.
    – s_ha_dum
    Sep 27, 2013 at 22:22
  • 2
    if you look at the action reference, it says the current user isn't yet authenticated at after_setup_theme, I'd say the earliest you can reliably do what you're trying to do is on init, but outputting markup there doesn't make sense. if you can explain what the goal is, it would help.
    – Milo
    Sep 27, 2013 at 22:34
  • I'm legitimately trying to understand how to use the public.php file within the WordPress-Plugin-Boilerplate with the use of typical queries and such. Any help would be great! Sep 27, 2013 at 22:56

1 Answer 1

1
require_once( wp_normalize_path(ABSPATH).'wp-load.php');

class Your_Plugin_Class {
    private  $is_user_logged_in;

    add_action('init', function(){
        $this-> is_user_logged_in = is_user_logged_in();
        echo 'is_user_logged_in'.$this->is_user_logged_in;
    });

    add_filter('woocommerce_billing_fields-2',array( $this, 'add_billing_field' ));

   }

    public function add_billing_field( $fields = array() ) {
        if ( $this->is_user_logged_in  ){
            echo 'add_billing_field'.$this->is_user_logged_in;
            return $fields;
        }else{
            echo 'add_billing_field'.$this->is_user_logged_in;
        }
    }
}
1
  • I seem to recall that you might have to 'delay' some functions in your plugin until after the admin stuff fully loads. Can't find that in any of my notes though. But it could be that your function is being called too early. Perhaps enable your plugin later with a high value for your add_filter? Aug 10, 2017 at 17:37

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.