wp query - Co-authors list of all posts

admin2025-06-04  3

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.

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.

Share Improve this question edited Feb 14, 2017 at 10:35 Max Yudin 6,3982 gold badges26 silver badges36 bronze badges asked Feb 14, 2017 at 10:04 Jan Hanzman KohoutekJan Hanzman Kohoutek 314 bronze badges 2
  • What is ID in the get_the_author_meta() and why are you not giving the field parameter to this function? – Max Yudin Commented Feb 14, 2017 at 10:43
  • this is main wordpress codex to get author ID . What do you mean by field param? – Jan Hanzman Kohoutek Commented Feb 14, 2017 at 10:56
Add a comment  | 

3 Answers 3

Reset to default 2

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!

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)
));
/**
 * @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;

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749010633a315590.html

最新回复(0)