admin管理员组

文章数量:1390334

I have a "real" cron job that executes wp-cron.php every now or then. I collect the link in to a variable by $link = get_edit_post_link(ID);

It works flawlessly if I am logged in and execute cron manually with wp-cron.php but when it is executed by schedule it wont collect the link and leaves it empty. Striped down version of code:

foreach($published_posts as $post_to_private){
    $user_data = get_userdata( $post_to_private->post_author);        
    $email = $user_data->user_email;
    $link = get_edit_post_link( $post_to_private->ID);

    $email = WP_Mail::init()
      ->to($email)
        "X-Mailer: PHP/". phpversion(),
        "Content-type: text/html; charset=utf-8",
    ])

      ->subject('test')
      ->template(plugin_dir_path( __DIR__ ) .'email-templates/expired-transit-email.php', [

        'link'          => $link,

    ]);


        //for testing of email

        return $email->send();

Any ideas?

I have a "real" cron job that executes wp-cron.php every now or then. I collect the link in to a variable by $link = get_edit_post_link(ID);

It works flawlessly if I am logged in and execute cron manually with wp-cron.php but when it is executed by schedule it wont collect the link and leaves it empty. Striped down version of code:

foreach($published_posts as $post_to_private){
    $user_data = get_userdata( $post_to_private->post_author);        
    $email = $user_data->user_email;
    $link = get_edit_post_link( $post_to_private->ID);

    $email = WP_Mail::init()
      ->to($email)
        "X-Mailer: PHP/". phpversion(),
        "Content-type: text/html; charset=utf-8",
    ])

      ->subject('test')
      ->template(plugin_dir_path( __DIR__ ) .'email-templates/expired-transit-email.php', [

        'link'          => $link,

    ]);


        //for testing of email

        return $email->send();

Any ideas?

Share Improve this question asked Feb 16, 2020 at 7:36 kru-xkru-x 415 bronze badges 1
  • "It works flawlessly if I am logged in" - because get_edit_post_link() checks if the current user has the permissions to edit the post. So you're getting the expected behavior. – Sally CJ Commented Feb 16, 2020 at 13:02
Add a comment  | 

1 Answer 1

Reset to default 3

Solved by:

//Check and set/unset user to able the script to run as admin
wp_set_current_user(1);
$link = get_edit_post_link($post_to_private->ID);
wp_set_current_user(0);

Thanks Sally CJ,

本文标签: pluginsgeteditpostlink() not working on wpcron