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 );
这是我在functions.php中的函数的开头(是现在, 根据帮助程序的请求现在是完整的代码), 我发疯了, 因为如果我使用相同的get_post_meta OUTSIDE函数INSIDE functions.php =>, 只要我能正常使用传递正确的帖子ID。但是一旦它在被钩住的函数内部(是的, 有一个钩子, 其余的都可以正常工作), 它就不会返回值。请帮忙
#1
你可能需要将$ post以及$ post_id传递给函数。尝试将第一行更改为:
function notify_new_posts( $post_id, $post ) {
参考:http://codex.wordpress.org/Plugin_API/Action_Reference/publish_post
#2
使用全局$ post;
例如:
function notify_new_posts($post_id) {
global $post;
评论前必须登录!
注册