0

I am using the Co-authors plus plugin on our web page because sometimes we have more authors of an article. It is working really fine until today. I used classic archive.php to list all posts of the author based on global settings (same post excerpt, thumbs, posts per page 6 – defined in global WordPress & theme PHPs). My authors requested to create theme profile pages with the list of all posts based on author.php. So I was forced to overwrite global settings (posts per page 6) with new WP_Query and here comes the problem with Co-authors plugin.

When I use the following code below the posts are listed only under author who is assigned as the main author but not listed under co-authors.

<?php
$the_query = new WP_Query(
    array( 
        'posts_per_page' => -1,
        'author' => get_the_author_meta('ID'),
    )
);

if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();

        the_time('d.m.Y ');
        the_permalink();
        the_title();

    endwhile;
    wp_reset_postdata();
else :
    _e( 'Sorry, no posts matched your criteria.' );

I tried to do it based on taxonomy, author_name, coauthor_meta, etc, nothing helps.

You can see example here (sorry it's not in English): This article is not listed under both authors.

  • What is ID in the get_the_author_meta() and why are you not giving the field parameter to this function? – Max Yudin Feb 14 '17 at 10:43
  • this is main wordpress codex to get author ID . What do you mean by field param? – Jan Hanzman Kohoutek Feb 14 '17 at 10:56
1

Nevermind, I fixed it by changing query by author_name instead of ID Works!

$the_query = new WP_Query(
array( 
    'posts_per_page' => -1,
    'author_name' => get_the_author_meta('nickname'),
)
);

We can close this question!

0

but if you want to use the list of co-authors to query posts in a plugin, you must use

get_the_author_meta( 'user_nicename', get_current_user_id() );

$locations = new WP_Query(array(
    'post_type' => 'location',
    'posts_per_page' => -1,
    'author_name' =>  get_the_author_meta('user_nicename', get_current_user_id())  //FILTER POSTS BY CO-AUTHORS (Co-Author-Plus Plugin)
));
0
/**
 * @param $id
 * @param int $paged (if $paged = null returl all posts)
 * @return array|bool
 */

function get_coauthor_posts_by_author_id($id, $paged = 1)
{
    global $wpdb;

    $slug = 'cap-' . strtolower(get_userdata($id)->user_nicename);


    $max_post = get_option('posts_per_page');
    $post_lower_limit = 0;


    if ($paged > 1) {
        $post_upper_limit = $max_post * $paged;
        $post_lower_limit = $post_upper_limit - $max_post;

    }


    $term_id = $wpdb->get_results($wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE slug = %s ", $slug))[0]->term_id;


    $sql = "SELECT SQL_CALC_FOUND_ROWS  $wpdb->posts.id FROM $wpdb->posts  LEFT JOIN $wpdb->term_relationships AS tr1 ON ($wpdb->posts.id = tr1.object_id) LEFT JOIN $wpdb->term_taxonomy ON ( tr1.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id ) WHERE 1=1  AND (($wpdb->posts.post_author = %d OR ($wpdb->term_taxonomy.taxonomy = 'author' AND $wpdb->term_taxonomy.term_id = %d))) AND $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') GROUP BY $wpdb->posts.id  ORDER BY $wpdb->posts.post_date DESC";
    $sql_limit = " LIMIT %d, %d";
    if ($paged !== null) {
        $sql = $sql . $sql_limit;
    }


    if ($term_id !== null) {
        $result = $wpdb->get_results($wpdb->prepare($sql, $id, $term_id, $post_lower_limit, $max_post), ARRAY_A);
        return array_column($result, 'id');
    }

    return false;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.