WordPress.org

Ready to get started?Download WordPress

Forums

Co-Authors Plus
co-authors plus not working when filtering by category. Here the solution (1 post)

  1. leoloso
    Member
    Posted 1 year ago #

    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:

    add_filter('pre_get_posts','set_cat_with_coauthors');
    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:

    http://wordpress.org/support/topic/plugin-co-authors-plus-cant-query-posts-for-category-and-author-together

    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)";

    add:

    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

    add:

    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.

    Cheers,
    Leo

    http://wordpress.org/extend/plugins/co-authors-plus/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.