[wp-trac] [WordPress Trac] #39321: Issue with get_permalink when using wp_update_post() and %author% is part of your permalink

WordPress Trac noreply at wordpress.org
Sat Dec 17 21:39:13 UTC 2016


#39321: Issue with get_permalink when using wp_update_post() and %author% is part
of your permalink
--------------------------+-----------------------------
 Reporter:  mauteri       |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  4.7
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 This was an odd and very specific issue I found with `get_permalink()`.
 When using `wp_update_post()`, I was getting the following: PHP Notice:
 Trying to get property of non-object etc... I traced it to `wp-includes
 /link-template.php` on line 205:

 {{{#!php
 if ( strpos($permalink, '%author%') !== false) {
         $authordata = get_userdata($post->post_author);
         $author = $authordata->user_nicename;
 }
 }}}

 When the post is a `revision`, post_author is equal to 0, so there's no
 `user_nicename` property because get_userdata(0) does not return an
 object. So here's a possible fix for those situations:

 {{{#!php
 if ( strpos($permalink, '%author%') !== false) {
         $authordata = get_userdata($post->post_author);
         if ( is_a( $authordata, 'WP_User' ) ) {
                 $author = $authordata->user_nicename;
         }
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39321>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list