additional posts on author.php (meta_query, coauthor)

Actually answering this question should be fairly simple, with little wp-knowhow...I just can't figure out what's going wrong:

I added coauthor support and coded a plugin for this purpose. Now everything is working fine except for that i'd like to show the posts of the second author on the second author's author.php as well...

My setup is that I added coauthor="ID" to the post_meta and now I'd like to return all posts on author.php where the current author is the main author (author => ID) and also where the meta data coauthor => "current author ID".

Getting all posts where author => ID is not problem. I am struggling with the other part. I have come up with a query like this:

$postauthor = get_user_by( 'slug', get_query_var( 'author_name' ) );
$postauthor = $postauthor->ID;

$args = array(  'post_type' => 'post',
                'meta_key' => 'coauthor',
                'meta_query' => array(
                  array(
                    'key' => 'coauthor',
                    'value' => $postauthor,
                    'compare' => 'LIKE',
                  )
                )
                  );
$query = new WP_Query( $args );

Coauthor is stored as longtext in the database.

  • Michelle Shull

    Hi, Markus!

    I was just fiddling with author.php yesterday on my day off, but sadly this is a bit above what I'm comfortable guiding others with. So I went to the Googles, let's see if any of this info helps:

    https://wordpress.org/support/topic/plugin-co-authors-plus-co-author-posts-not-showing-in-author-archive <-this post is actually about extending co-authors to show co-authored posts on author.php, so I think the hints here may be applicable to what you're doing.

    This might also have some tidbits that may help: http://wordpress.stackexchange.com/questions/88548/editing-theme-to-apply-co-authors-plus

    I see a lot of posts saying CoAuthors will create co-author pages, but in my experience it's never worked quite like that, my other suggestion would have been to take CoAuthors apart to see how they handle co-authors on archive pages.

    If this doesn't get things going in the right direction, we can ask our code gurus for a little more guidance here. I'm very interested to see how this develops, keep us posted one way or another!

  • Markus

    Hi Michelle,

    thank you for your answer.
    Well so far I figured out that the querry which is generated by the following code does actually work on the database:

    $args = array(  'post_type' => 'post',
                    'meta_key' => 'coauthor',
                    //'author' => 34,
                    'orderby' => 'coauthor',
                    'order' => 'ASC',
                    'meta_query' => array(
                      array(
                        'key' => 'coauthor',
                        'value' => $postauthor,
                        'compare' => '=',
                        'type' => 'numeric'
                      )
                    )
                      );
    $query = new WP_Query( $args );

    That's the query it generates:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ( wp_postmeta.meta_key = 'coauthor' AND ( ( mt1.meta_key = 'coauthor' AND CAST(mt1.meta_value AS SIGNED) = '27' ) ) ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value ASC LIMIT 0, 10

    However, somehow wordpress does not show the correct posts because the subsequent querries don't get the correct post ids.: 3167 and 2816 (which are returned by the query above)

    SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (2560,2550,2412,2425,2396,2359,939,1697) ORDER BY meta_id ASCSELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (wp_posts.post_author = 27) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 10

    Those numbers seem to be wrong 2560,2550,2412,2425,2396,2359,939,1697 also the query does not run on the database. There should be only two.

    So somehow there must be something in the loop that goes wrong....
    An additional interesting fact is, that when i take the // away from the uncommented line above, the query does return the single post, for which the condition author = 34 and coauthor = 27 is true...

    I would much appreciate if some of you programming guys could provide some insight into the loop of wordpress...I assume that this must be a rather straight forward fix,...

  • Jude

    Hi there @Markus

    Sorry the response took a while longer than expected. Appreciate your patience. Looks like Michelle is really busy and this one slipped through the gaps, I'm gonna take the liberty of chipping in here.

    This looks like a clear case of The Loop not being reset.

    Add this at the end of the loop to get the desired results. For example

    $my_query = new WP_Query( $query );
    query_posts( my_query);
    if ( have_posts() ) : while ( have_posts() ) : the_post();
     {
        // Do stuff here
    }
    wp_reset_postdata();

    Hope that helps

    Cheers
    Jude

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.