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

Wordpress get_post_meta不在functions.php中的函数内部工作,但在函数外部工作,wordpress,wordpress-theming,Wordpress,Wordpress Theming,这是一个开始,,现在它的完整代码是根据我函数内的helper请求编写的。php我快疯了,因为如果我在函数内的函数外使用相同的get_post_meta。php=>只要我传递了正确的post id,它就可以完美地工作。但是一旦它在被钩住的函数内,是的,就有一个钩子,它的其余部分就不能完美地工作返回一个值。请帮助您可能需要将$post以及$post\u id传递给函数。请尝试将第一行更改为: function notify_new_posts($post_id) { $keyval = get_

这是一个开始,,现在它的完整代码是根据我函数内的helper请求编写的。php我快疯了,因为如果我在函数内的函数外使用相同的get_post_meta。php=>只要我传递了正确的post id,它就可以完美地工作。但是一旦它在被钩住的函数内,是的,就有一个钩子,它的其余部分就不能完美地工作返回一个值。请帮助

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

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 );
参考:

使用全局$post

例:


你用的是什么钩子?你能粘贴完整的代码吗?我发布了完整的代码,希望你能帮我。我真的疯了,我需要更改什么吗?因为它不起作用,所以我将$post\u id更改为$post->id,但它不起作用。请添加一个解释好吗?比如为什么你会在你的案例中得到后元工作。
function notify_new_posts( $post_id, $post ) {
function notify_new_posts($post_id) {
global $post;