Automattic / Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
https://wordpress.org/plugins/co-authors-plus/
GNU General Public License v2.0
291 stars 204 forks source link

Missing post_name on author archives #730

Closed jonathanstegall closed 4 years ago

jonathanstegall commented 4 years ago

I'm not sure if this is new or if I've just not noticed it before. When I load an author archive on my local install (running version 3.4.3), I'm seeing this:

[22-Apr-2020 18:21:49 UTC] PHP Notice:  Undefined property: stdClass::$post_name in /wp-includes/template.php on line 436

Doing an error_log on $post at that template.php file returns:

[22-Apr-2020 18:21:49 UTC] post is stdClass Object
(
    [ID] => 
    [display_name] => 
    [first_name] => 
    [last_name] => 
    [user_login] => 
    [user_email] => 
    [linked_account] => 
    [website] => 
    [description] => 
    [twitter] => 
    [job-title] => 
    [teaser] => 
    [user_nicename] => 
    [type] => guest-author
)

I took out the individual values from those keys, but they're all what I would expect for an author. I can see that there is a post_name key in the database. I think I would've noticed this before, but I don't remember ever seeing it, so maybe it's a new issue?

rebeccahum commented 4 years ago

@jonathanstegall Hi, do you have a stack trace by any chance for that notice? Is it just viewing any author archive to reproduce?

jonathanstegall commented 4 years ago

Sorry for my delay here. I've not been able to reproduce it again. I think it may have been conflicting with a third party plugin's problems, which have since been fixed. I think this can be closed.

timbolimboslice commented 8 months ago

@rebeccahum I'm running into this issue on a site. We are using Co-Authors for author archive pages (example) and see this warning come up whenever we use template related WordPress functions like get_page_template() and get_page_template_slug().

I'm able to get around this by using this filter, but I wonder if this should be resolved in the plugin instead of a filter:

/**
 * Adds any filters necessary for the Co-Authors Plus Plugin.
 */
class CoAuthorsPlusManager {

    /**
     * Runs this manager.
     */
    public function run() {
        add_filter( 'coauthors_guest_author_fields', array( $this, 'add_post_name_property' ), 10, 1 );
    }

    /**
     * Adds the post_name property to the guest author fields.
     * This doesn't do anything, but prevents PHP warnings from firing on an individual contributor page.
     *
     * @param array $fields_to_return The fields to return.
     *
     * @return array
     */
    public function add_post_name_property( $fields_to_return ) {
        if ( is_array( $fields_to_return ) ) {
            array_push(
                $fields_to_return,
                array(
                    'key'   => 'post_name',
                    'label' => '',
                    'group' => 'hidden',
                    'input' => 'hidden',
                ),
            );
        }
        return $fields_to_return;
    }
}