Support » Plugin: Co-Authors Plus » co-authors plus not working when filtering by category. Here the solution

  • Hi,

    I want to show my author’s posts (included co-written ones) in the author’s profile page. If I filter the posts by category, like this:

    function set_cat_with_coauthors($query) {
    	$query->set('cat', 1107);
        return $query;

    it doesn’t work, it doesn’t bring the co-authored posts.

    I found a solution (and an explanation why it happens) to this problem here:

    This is for an older version of Co-Authors Plus. I edited it for version 3.0.5 and it works fine, the code is the following. In file co-authors-plus.php:

    function posts_join_filter, after line:

    // Check to see that JOIN hasn't already been added. Props michaelingp and nbaxley
    $term_relationship_join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";


    if ($query->get('cat')) {
    	$term_relationship_join = " INNER JOIN {$wpdb->term_relationships} AS coauthor_relationships ON ({$wpdb->posts}.ID = coauthor_relationships.object_id) INNER JOIN {$wpdb->term_taxonomy} AS coauthor_taxonomy ON (coauthor_relationships.term_taxonomy_id = coauthor_taxonomy.term_taxonomy_id)";

    function posts_where_filter, after line:

    $where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*=\s*(\d+))/', '(' . $maybe_both_query . ' ' . $terms_implode . ')', $where, 1 ); #' . $wpdb->postmeta . '.meta_id IS NOT NULL AND


    if ($query->get('cat')) {
    	$where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*=\s*(\d+))/', '($1 OR (coauthor_taxonomy.taxonomy = \''. $this->coauthor_taxonomy.'\' AND coauthor_taxonomy.term_id = \''. $term->term_id .'\'))', $where, 1); #' . $wpdb->postmeta . '.meta_id IS NOT NULL AND

    I added “if ($query->get(‘cat’))” to modify the $term_relationship_join and $where variables since, otherwise, when it is not filtering by category it doesn’t work, so keep it the way it was.

    It would be cool to have this fix merged into the plugin.


  • The topic ‘co-authors plus not working when filtering by category. Here the solution’ is closed to new replies.