• 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 just WHERE wp_posts.post_author = .... I fixed this by changing the regex in posts_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 function fix_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.

    • This topic was modified 6 years, 8 months ago by Sean Leavey. Reason: added code blocks
    • This topic was modified 6 years, 8 months ago by Sean Leavey.
Viewing 11 replies - 1 through 11 (of 11 total)
  • G P

    (@wetapplemedia)

    Which files are these modifications made to?

    Thread Starter Sean Leavey

    (@seanleavey)

    To co-authors-plus.php in the plugin’s directory.

    I’m having the same issue and tried this fix, but still not seeing posts on the author Archive for Authors that are the coauthors. I can get those by adding a modified WP_Query to the author page, but then it only shows posts where they are co-authors(either first or second), and not the posts where they are just the single author.

    Would rather not have to buy Bylines…. Any other thoughts? Thank You Sean.

    • This reply was modified 6 years, 8 months ago by silasmiller. Reason: clarification
    Thread Starter Sean Leavey

    (@seanleavey)

    There may be other queries that have changed in WordPress core since this plugin was written, preventing the extra join from the coauthor terms being searched against. One such instance of a query changing in WordPress already caused the issue I fixed above – maybe there are more.

    How confident are you about hacking around the code? If you can get your installation to spit out the content of the queries being modified by Co-Authors Plus, that would be useful to help debug your problem. The error_log function is useful to print stuff to the log file in /wp-content/debug.log. The first places to look would be the values of $where in posts_where_filter, $groupby in posts_groupby_filter and $join in posts_join_filter, immediately before they return their value.

    Interesting. I just looked and it seems that Co-Authors isn’t changing the query for author.php, which is why I tried using WP_Query, but I can’t seem to use ‘OR’ to go for a taxonomy and normal author . Maybe I’m missing something.

    Could it be related to the twentyeleven_posted_on function? I’ve modified that a couple different ways but it doesn’t seem to help.

    I’m happy to hack or write new code, but I feel like WordPress obfuscates enough that it’s hard compared to normal MVC systems I’m used to.

    Thread Starter Sean Leavey

    (@seanleavey)

    Easiest way to check is to load a different theme temporarily and see if it’s still broken.

    Ah it seems that the plugin doesn’t add the taxonomy to all the existing posts. So it only shows the ones that have been saved since it was installed. Using a custom WP_Query I can get the posts with the co-author set to either the 1st or 2nd slot, but the old posts don’t have the taxonomy.

    I put this in my author.php file for my theme

    <?php 
    $user_name = get_the_author_meta('user_login');
    $args = array( 
        'tax_query' => array(
            array(
                'taxonomy' => 'author',
                'terms' => $user_name,
                'field' => 'name',            
            ),
                        ),
    );
    $author_query = new WP_Query( $args );
    ?>

    Then replaced the post loop with:

    <?php 
        while  ( $author_query->have_posts() ) : $author_query->the_post(); 
        ?>

    Now I just need to find a way to scan all the existing posts and add the taxonomy entry for them.

    Ideas? Seems like I’m going to have to just go write a MySQL query to accomplish.

    Thanks again!

    Thread Starter Sean Leavey

    (@seanleavey)

    It might be easier to use the existing functions in Co-Authors Plus to do this. It looks like the coauthor term is created when the post is saved by the coauthors_update_post function that is hooked onto save_post. Looking at that, it seems the actual function that creates the terms for the post is $coauthors->add_coauthors( $post_id, $coauthors ); (where $coauthors is the name of an instance of the CoAuthors_Plus class). If you run that function for each post id in your database that doesn’t already have a coauthor, with $coauthors set to an array containing the user login names of the coauthors you want to assign, that should work.

    • This reply was modified 6 years, 8 months ago by Sean Leavey.

    Interesting. I’m hesitant to try run it on all posts, but hey that’s a what a dev environment is for!

    Thanks again. I’ll let you know if that works. If it helps someone else with the issue that would be great.

    Thread Starter Sean Leavey

    (@seanleavey)

    Oops, that code snipped should be $coauthors->add_coauthors( $post_id, $array_of_coauthors );, i.e. the second parameter is not the class instance, but actually an array of WP_Author objects.

    I got it working by running this CLI script:
    wp --url=example.com co-authors-plus create-terms-for-posts which updates all the posts with the taxonomy. I also had to fix a blocking issue in the actual script class-wp-cli.php changing line 81 from

    WP_CLI::error
    to
    WP_CLI::line

    Then I could use my custom WP_Query and all looks to be working!

    This is the working query and loop for author.php:

    <?php 
    $user_name = get_the_author_meta('user_login');
    $args = array( 
        'tax_query' => array(
            array(
                'taxonomy' => 'author',
                'terms' => $user_name,
                'field' => 'name',            
            ),
                        ),
    );
    $author_query = new WP_Query( $args );
    while  ( $author_query->have_posts() ) : $author_query->the_post(); 
    get_template_part('article', 'archive');
    endwhile; ?>

    Thanks again for pointing me in the right direction!

    • This reply was modified 6 years, 8 months ago by silasmiller.
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Two bug fixes for author page’ is closed to new replies.