get_post_meta不在functions.php中的函数内部工作,但在外部工作

时间:2014-07-24 11:06:54

标签: wordpress wordpress-plugin wordpress-theming

function notify_new_posts($post_id) {


$keyval = get_post_meta( $post_id);  


$buyer_wdistance   = get_post_meta( $post_id, 'cf_wdrive');


//$buyer_wdistance   =   $keyval['cf_wdrive'][0];
$buyer_location    =   $keyval['cf_address'][0]." , ".$keyval['cf_zipcode'][0];


//$buyer_location    = '3302 23rd street, astoria, ny';
//$buyer_wdistance   = '10';

        $post          = get_post($post_id);
        $author        = get_userdata($post->post_author);
        $author_email  = $author->user_email;
        $email_subject = " New car request posted ";





$blogusers = get_users( 'blog_id=1&orderby=nicename&role=dealership' );

foreach ( $blogusers as $user ) {


$target_email    =  $user->user_email;

$current .= " targetemail: ".$target_email ." | " ;



$dealer_location = get_usermeta($user->id,'company_name')." , ".get_usermeta($user->id,'rep_name') ; 



 $current .= " dealerlocation: ".$dealer_location . " | " ;


 $from = $buyer_location;
//echo "<br>";
 $to   = $dealer_location;


$from = urlencode($from);
$to   = urlencode($to);
$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
$data = json_decode($data);
$time     = 0;
$distance = 0;

foreach($data->rows[0]->elements as $road) {
    $time += $road->duration->value;
    $distance += $road->distance->value;
}

$distance = round($distance*0.000621371);
$time = round($time/60);
//echo "distance to this dealer in miles ".$distance."<br>";

$current .= " distance: ".$distance . " | " ;





if ($distance  <= $buyer_wdistance ){  

        ob_start(); ?>

        <html><head><title>New post at <?php bloginfo( 'name' ) ?></title>
            </head> <body>
                <p>Hello <?php echo $author->user_firstname ?>, </p>
                <p>A new lead was posted: <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a> .</p>
            </body></html>

        <?php

        $message = ob_get_contents();   ob_end_clean();

   wp_mail( $target_email, $email_subject, $message );

} //end of if statememnt

}

$file = 'people.txt';
// Open the file to get existing content
//$current = file_get_contents($file);
// Append a new person to the file

$current .= " postid: ".$post_id." | " ;
$current .= " author: ".$post->post_author       . " | " ;
$current .= " author_email ".$author_email       . " | " ;
$current .= " buyerlocation: ".$buyer_location.$buyer_wdistance  . " | " ;

$current .= " buyerwdistance: ".$buyer_wdistance . " | " ;



// Write the contents back to the file
file_put_contents($file, $current);




}

add_action( 'publish_post', 'notify_new_posts', 10, 2 );

这是我在function.php中的函数的开头(现在,它是完整的代码,根据帮助请求)我疯了因为如果我使用相同的get_post_meta OUTSIDE函数INSIDE functions.php =&gt;只要我传递正确的帖子ID,它就能很好地工作。但是一旦它在钩住的函数内部(是的,有一个钩子,其余部分完美地工作)它不会返回一个值。请帮忙

2 个答案:

答案 0 :(得分:0)

您可能需要将$post传递给该函数以及$post_id。尝试将第一行更改为:

function notify_new_posts( $post_id, $post ) {

参考:http://codex.wordpress.org/Plugin_API/Action_Reference/publish_post

答案 1 :(得分:0)

使用global $post;

例如:

function notify_new_posts($post_id) {
global $post;