Skip Navigation

[Resolved] Filter post by Author (Co-Authors Plus)

This thread is resolved. Here is a description of the problem and solution.

Problem:

In posts view, setup filter by author settings from "Co-Authors Plus" plugin:

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

Solution:

The "Co-Authors Plus" plugin is using a hidden custom taxonomy "author" to store the "Co-Authors" values, but Views author filter only works with wordpress built-in post field "post_author", see wordpress document:

https://codex.wordpress.org/Database_Description#Table:_wp_posts

So there isn't such a built-in feature within Views plugin, it needs custom codes, here is an example codes:

https://toolset.com/forums/topic/filter-post-by-author-co-authors-plus/#post-670111

Relevant Documentation:

https://codex.wordpress.org/Database_Description#Table:_wp_posts

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by Luo Yang 6 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#646005

I am trying to: Filter post by author.

Link to a page where the issue can be seen: hidden link

I expected to see: Filter results by selecting an author. It works well if the author is only single.

Instead, I got: It's working just fine if it's only single author. If the post have two authors (using co-authors plus), it only want to filter the first Authors.
to test : try to select (Abraham or Eli). Each of them belongs to 3 diff post.

What is strange to me, it works just fine on the "Author's Archive page".
hidden link
hidden link

note : I created Author's list query in the function.php
"Views" I'm using for that page: "Global - Post"

I refer to this docs :
https://toolset.com/forums/topic/parametric-search-by-author/
https://toolset.com/forums/topic/co-authors-conditional-output/
https://toolset.com/forums/topic/co-authors-plus-displaying-fields/
http://plugins.svn.wordpress.org/co-authors-plus/trunk/template-tags.php

#646634

Hello,

I assume we are talking about the "Co-Authors Plus" plugin download it from here:
https://wordpress.org/plugins/co-authors-plus/

I have tried it in my localhost, it is using a hidden custom taxonomy "author" to store the "Co-Authors" values, but Views author filter only works with wordpress built-in post field "post_author", see wordpress document:
https://codex.wordpress.org/Database_Description#Table:_wp_posts

So there isn't such a built-in feature within Views plugin, it needs custom codes, you may try to check it with our experienced contractors:
https://toolset.com/contractors/

#649476

Is it possible for you to look at my code, as I think I'm getting closer with this. Because the function works just fine with the built in author archive page. It showing the correct posts.

note : where can I share my credentials?

Thank you in advanced.

#650717

I have enabled the private detail box, you can share your code in it. Thanks

#670111

As I mentioned above, it needs custom codes, I have setup an example in your website, edit the codes in your theme/functions.php, as below:
1) Replace line 123, from:

$return .= '<option value="' . $author->ID . '" ' . $selected . '>' . $author->display_name . '</a>';

To:

				$selected = '';
				if(isset($_GET['author-filter']) && !empty($_GET['author-filter'])){
					$selected = selected( $_GET['author-filter'], $author->ID, false );
				}
				$return .= '<option value="' . $author->ID . '" ' . $selected . '>' . $author->display_name . '</a>';

It will select the author option according to URL parameter "author-filter", after you submit the custom search form.

More help:
https://developer.wordpress.org/reference/functions/selected/

2) Add below codes to apply the taxonomy "author" filter, line 201~230:


add_filter('wpv_filter_query', 'Co_Authors_Plus_func', 99, 3);
function Co_Authors_Plus_func($query, $view_settings,$view_id ) {
	global $coauthors_plus;
    if($view_id == 20324) //replace view ID
    {
        if(isset($_GET['author-filter']) && !empty($_GET['author-filter'])){
			$user = get_user_by( 'id', $_GET['author-filter'] );
			$user_login = $user->user_login;
			$query['tax_query'][] = array(
				'taxonomy' => $coauthors_plus->coauthor_taxonomy,
				'field'    => 'slug',
				'terms'    => 'cap-' . $user->user_login,
			);
 			foreach($query as $k => $v){
				if($k == 'author__in' || $k == 'author'){
					unset( $query[$k] );
					break;
				}
			} 
 			foreach($query as $k => $v){
				if($k == 'post__in' ){
					unset( $query[$k] );
					break;
				}
			} 
			print_r($query);
		}
    }
    return $query;
 }

More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

And it is only an example, you will need to customize the codes according to your website settings.

#695202

Thank you Luo for all your help.

#695363

You are welcome