WordPress.org

Forums

Co-Authors Plus
Adding compatibility to filter by more than 1 author (2 posts)

  1. leoloso
    Member
    Posted 11 months ago #

    Hi,

    Right now the plug-in works with queries filtering by 1 author (query arg author=939)

    But I need to filter by many authors (eg: query arg author=939,851), so I added few changes that I detail below.

    In addition, I also fix 2 other problems:

    - It only works in the Author page, but I also need it to filter by author in any other page
    - It doesn't work when also filtering by category

    Below all my changes to fix these issues. I hope they can be added to the main code

    file: co-authors-plus.php

    function posts_join_filter( $join, $query ){
    		global $wpdb;
    
    		// Hack: what matters is if we're filtering by author, so check on this one condition on the query (in addition to if it's the author page)
    		// if( $query->is_author() ) {
    		if ($query->is_author() || $query->query_vars['author']) {
    
    			if ( !empty( $query->query_vars['post_type'] ) && !is_object_in_taxonomy( $query->query_vars['post_type'], $this->coauthor_taxonomy ) )
    				return $join;
    
    			if ( empty( $this->having_terms ) )
    				return $join;
    
    			// 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)";
    
    			// Hack GreenDrinks: allow for Co-Authors plus to work together with categories
    			// Solution found here: http://wordpress.org/support/topic/plugin-co-authors-plus-cant-query-posts-for-category-and-author-together
    			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)";
    			}
    
    			$term_taxonomy_join = " INNER JOIN {$wpdb->term_taxonomy} ON ( {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id )";
    
    			if( strpos( $join, trim( $term_relationship_join ) ) === false ) {
    				$join .= str_replace( "INNER JOIN", "LEFT JOIN", $term_relationship_join );
    			}
    			if( strpos( $join, trim( $term_taxonomy_join ) ) === false ) {
    				$join .= str_replace( "INNER JOIN", "LEFT JOIN", $term_taxonomy_join );
    			}
    		}
    
    		return $join;
    	}
    function posts_where_filter( $where, $query ){
    		global $wpdb;
    
    		// Hack: what matters is if we're filtering by author, so check on this one condition on the query (in addition to if it's the author page)
    		// if ( $query->is_author() ) {
    		if ($query->is_author() || $query->query_vars['author']) {
    
    			if ( !empty( $query->query_vars['post_type'] ) && !is_object_in_taxonomy( $query->query_vars['post_type'], $this->coauthor_taxonomy ) )
    				return $where;
    
    			// Hack GreenDrinks: if more than 1 author, then do the replacement for each one of them
    			// if ( $query->get( 'author_name' ) )
    			// 	$author_name = sanitize_title( $query->get( 'author_name' ) );
    			// else {
    			// 	$author_data = get_userdata( $query->get( 'author' ) );
    			// 	if ( is_object( $author_data ) )
    			// 		$author_name = $author_data->user_nicename;
    			// 	else
    			// 		return $where;
    			// }
    			$author_names = array();
    			if ( $query->get( 'author_name' ) )
    				$author_names = array(sanitize_title( $query->get( 'author_name' ) ));
    			elseif ($author = $query->get( 'author' )) {
    				$author = explode(',', $author);
    				foreach ($author as $author_id) {
    					$author_data = get_userdata( $author_id );
    					if ( is_object( $author_data ) )
    						$author_names[] = $author_data->user_nicename;
    					else
    						return $where;
    				}
    			}
    
    			// Hack GreenDrinks: move outside, so it can have many terms for each author, without overriding
    			$this->having_terms = '';
    
    			// Hack GreenDrinks: addition of the for loop, to do this for each author
    			foreach ($author_names as $author_name) {
    
    				$terms = array();
    				$coauthor = $this->get_coauthor_by( 'user_nicename', $author_name );
    				if ( $author_term = $this->get_author_term( $coauthor ) )
    					$terms[] = $author_term;
    				// If this coauthor has a linked account, we also need to get posts with those terms
    				if ( ! empty( $coauthor->linked_account ) ) {
    					$linked_account = get_user_by( 'login', $coauthor->linked_account );
    					if ( $guest_author_term = $this->get_author_term( $linked_account ) )
    						$terms[] = $guest_author_term;
    				}
    
    				// Whether or not to include the original 'post_author' value in the query
    				// Don't include it if we're forcing guest authors, or it's obvious our query is for a guest author's posts
    				if ( $this->force_guest_authors || stripos( $where, '.post_author = 0)' ) )
    					$maybe_both = false;
    				else
    					$maybe_both = apply_filters( 'coauthors_plus_should_query_post_author', true );
    
    				$maybe_both_query = $maybe_both ? '$1 OR' : '';
    
    				if ( !empty( $terms ) ) {
    					$terms_implode = '';
    					// Hack GreenDrinks: move outside, so it can have many terms for each author, without overriding
    					// $this->having_terms = '';
    					foreach( $terms as $term ) {
    						$terms_implode .= '(' . $wpdb->term_taxonomy . '.taxonomy = \''. $this->coauthor_taxonomy.'\' AND '. $wpdb->term_taxonomy .'.term_id = \''. $term->term_id .'\') OR ';
    						$this->having_terms .= ' ' . $wpdb->term_taxonomy .'.term_id = \''. $term->term_id .'\' OR ';
    					}
    					$terms_implode = rtrim( $terms_implode, ' OR' );
    					// Hack GreenDrinks: move outside, so it can have many terms for each author, without overriding
    					// $this->having_terms = rtrim( $this->having_terms, ' OR' );
    
    					$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
    					// Hack GreenDrinks: addition line below: when users are selected thru query_vars['author'], the WHERE statement is an IN, not an =
    					$where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*IN\s*\(([,0123456789]+\)))/', '(' . $maybe_both_query . ' ' . $terms_implode . ')', $where, 1 ); #' . $wpdb->postmeta . '.meta_id IS NOT NULL AND
    
    					// Hack GreenDrinks: allow for Co-Authors plus to work together with categories
    					// Solution found here: http://wordpress.org/support/topic/plugin-co-authors-plus-cant-query-posts-for-category-and-author-together
    					if ($query->get('cat')) {
    
    						// Hack GreenDrinks: when users are selected thru query_vars['author'], the WHERE statement is an IN, not an =
    						$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
    						$where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*IN\s*\(([,0123456789]+\)))/', '($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
    					}
    				}
    			}
    
    			// Hack GreenDrinks: move outside, so it can have many terms for each author, without overriding
    			$this->having_terms = rtrim( $this->having_terms, ' OR' );
    		}
    		return $where;
    	}
    /**
    	 * Modify the author query posts SQL to include posts co-authored
    	 */
    	function posts_groupby_filter( $groupby, $query ) {
    		global $wpdb;
    
    		// Hack: what matters is if we're filtering by author, so check on this one condition on the query (in addition to if it's the author page)
    		// if( $query->is_author() ) {
    		if ($query->is_author() || $query->query_vars['author']) {
    
    			if ( !empty( $query->query_vars['post_type'] ) && !is_object_in_taxonomy( $query->query_vars['post_type'], $this->coauthor_taxonomy ) )
    				return $groupby;
    
    			if ( $this->having_terms ) {
    				$having = 'MAX( IF( ' . $wpdb->term_taxonomy . '.taxonomy = \''. $this->coauthor_taxonomy.'\', IF( ' . $this->having_terms . ',2,1 ),0 ) ) <> 1 ';
    				$groupby = $wpdb->posts . '.ID HAVING ' . $having;
    			}
    		}
    		return $groupby;
    	}

    https://wordpress.org/plugins/co-authors-plus/

  2. giruburuno
    Member
    Posted 9 months ago #

    hello !
    i'm a newbie here.

    i would like, next to a post, to display the total authors and co- authors of the category of the posts.

    what would be the function to display the count of all difference authors that contributed to the category of this post?

    THANK you so much if you could help.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.