Two bug fixes for author page
-
I discovered two issues with the author page. First of all, the author page does not show posts the author is coauthor on but not primary author. This appears to be a regression due to WordPress now sometimes using
WHERE wp_posts.post_author IN (...)
and not justWHERE wp_posts.post_author = ...
. I fixed this by changing the regex inposts_where_filter
from:$where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*=\s*(\d+))/', '(' . $maybe_both_query . ' ' . $terms_implode . ')', $where, -1 );
to
$where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*(?:=|IN)\s*\(?(\d+)\)?)/', '($1 OR ' . $terms_implode . ')', $where, 1 );
(I also set the maximum number of replacements to 1 from -1 (unlimited), as I think this is safer)
I also found that the author’s name is not set correctly if the first post retrieved for the author’s page is not a post where the author is the primary author, and the author’s page was requested by the query string
?author=...
where...
is their user ID. The functionfix_author_page
fixes this behaviour only when the query string is?author_name=...
where...
is their nicename.I fixed this by changing in
fix_author_page
:$author_name = sanitize_title( get_query_var( 'author_name' ) ); if ( ! $author_name ) { return; }
to
$author_id = absint( get_query_var( 'author' ) ); $author_name = sanitize_title( get_query_var( 'author_name' ) ); if ( isset( $author_id ) ) { // get author by id $author = $this->get_coauthor_by( 'id', $author_id ); } elseif ( isset( $author_name ) ) { // get author by specified name $author = $this->get_coauthor_by( 'user_nicename', $author_name ); } else { // no query variable was specified; not much we can do return; }
Hope this helps.
- The topic ‘Two bug fixes for author page’ is closed to new replies.