WordPress.org

Forums

[resolved] WP_Query in plugin throwing error (3 posts)

  1. Jesse Friedman
    Member
    Posted 1 year ago #

    I have a plugin that I'm writing. In the plugin I have the following class:

    class aks_gf_augmentation {
    
    	public function __construct() {
    
    		$zip_code = '02653'; //this is temporary, eventually to be set by field filled out in gravity form
    
    		$query = new WP_Query( array(
    			'post_type' => 'aks_zip_code',
    			'name' => $zip_code
    		) );
    		if( $query->have_posts() ) {
    			while ( $query->have_posts() ) {
    				$query->the_post();
    				$rep_to_send = get_post_meta( $post->ID, 'aks_target', true );
    			}
    		}
    		else {
    			$rep_to_send = 'default@domain.com';
    		}
    
    		add_filter("gform_pre_send_email", "before_email");
    		function before_email($email){
    
    			global $rep_to_send;
    			$email["to"] = $rep_to_send;
    			return $email;
    		}
    
    	}
    
    }
    
    $aks_gf_augmentation = new aks_gf_augmentation;

    The use of the 'post_type' argument in WP_Query throws this error:

    Fatal error: Call to undefined function get_userdata() in /nas/wp/www/staging/agmoil/wp-includes/query.php on line 3911

    If I remove the 'post_type' => 'amgo_zip_code' argument, the plugin doesn't return the correct post but it also doesn't thrown an error.

    Am I missing something? I've never seen this error arise from using WP_Query in a plugin. Also I have double and triple checked that the name of the custom_post_type does = aks_zip_code

    Any help would be greatly appreciated

  2. Jesse Friedman
    Member
    Posted 1 year ago #

    Also, I just figured out that the error only occurs when it actually finds a post and attempts to return it. If I alter any of the arguments to ensure no posts are queried, then no errors occur.

  3. Jesse Friedman
    Member
    Posted 1 year ago #

    I hate post midnight stupid mistakes.

    a.k.a. don't leave functions like that in the __construct...

Topic Closed

This topic has been closed to new replies.

About this Topic