Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am executing this code in my external php file (= I am accessing his externaly without loading the full WP; I have a reason why I am doing it) that is located here:

http://******.com/wp-content/themes/mycartheme/file.php

I have this code to check if the code exist in the post_meta table and if yes grab the post_id of that row and make another query to grab the custom field value rw_email_dealer of that post id.

define( 'SHORTINIT', true );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

global $wpdb;

$code = "2302327676";

$unique_check = $wpdb->get_var(
              "SELECT post_id 
              FROM {$wpdb->postmeta} 
              WHERE meta_key = 'rw_code' 
              AND meta_value = '{$code}' 
              LIMIT 1"
);

if ($unique_check == NULL) {
   die('Die!');
}
else {
    $email_dealer = get_post_meta($unique_check, 'rw_email_dealer', true);  
    print_r($email_dealer);
}

I can get the $unique_check (the post_id value). No problem here.

However, if I try this

$email_dealer = get_post_meta($unique_check, 'rw_email_dealer', true); 

I am getting the error:

Fatal error: Call to undefined function get_post_meta() 

So, I guess that the code for that function is not loaded with:

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

How can I solve that so I can use that function get_post_meta() in external php files that don't come with WordPress?

share|improve this question
    
As I see it, you'll have to get post meta directly from the database as you did with the post ID; because full WP hasn't loaded yet, and therefore WordPress hasn't queried the database yet. –  its_me Nov 12 '13 at 8:57

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.