Skip Navigation

[Resolved] Custom search: author field

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 12 replies, has 2 voices.

Last updated by Minesh 11 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2710925

Hi,

we´ve got a astro photo gallery and the people would like to filter by author, who posted the image post. The post is created by toolset and also the upload form. I´ve created a custom search: hidden link but I can´t find any field for search by author or filter by author.

How can I achieve this?

Best

#2710927

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Filtering view results based on the custom search using dropdown select of author is not supported.

However - Can you please confirm you want to list all users with dropdown select with author role or only the users belongs to XYZ post type?

But fortunately I have workaround for this. Can you please send me problem URL where you want to add the post author custom search filter as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2710929

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It seems you forget to answer the following question:

However - Can you please confirm you want to list all users with dropdown select with author role or only the users belongs to XYZ post type?

#2710931

Sorry, I would like to display only users which are attached to the post type "astrofoto" Is there only a dropdown or also a input field with search completion?

#2710943

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - each user is unique entity so I suggest you should go with dropdown select.

Can you please check now: hidden link

I've added the following custom shortcode to "Custom Code" section that will help to display the author dropdown select:
=> hidden link

add_shortcode("custom-search-author-filter", "func_get_all_authors_belongs_posttype");
function func_get_all_authors_belongs_posttype() {
  

  $out = '<select name="author-filter" class="wpcf-form-select form-select select">';
  $out = $out.'<option value="">Select Author</option>';
  $users = get_users();
  
  $users = get_users(array(
    'who' => 'authors',
    'has_published_posts' => array('astrofoto'),
     ));
  
  
  $author_filter = '';
  if(isset($_GET['author-filter'])){
      $author_filter = $_GET['author-filter'];
  }
  foreach ($users as $user) {
    $selected = '';
    if($author_filter == $user->ID){
        $selected = ' selected';
    }
    $out = $out.'<option value="' . $user->ID . '"' . $selected . '>' . $user->display_name . '</option>';
  }
  $out = $out."</select>";
  return $out;
}

To your view's search section I've added the shrotcode block and added the above shortcode as given under:
=> hidden link

[custom-search-author-filter]

And to your view I've added the post author query filter:

Post author filter
Select posts with the author's id determined by the URL parameter "author-filter" eg. yoursite/page-with-this-view/?author-filter=1

Screenshot: hidden link

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/legacy-features/views-plugin/filtering-views-query-by-author/#post-author-is-set-by-url-parameter

Can you please confirm it works as expected now.

#2710944

Thanks, there are different roles: subscriber and author. Can you please update the php script?

#2710946

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What you mean - Can you please explain in brief?

#2710947

The user filter contains two different roles: subscriber and author.

#2710949

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You mean instead of using the user name you want to display role names with Author filter?

#2710951

No. Currently at the author filter there are displayed 4 users, but there are plenty of others users. It is caused by
$users = get_users(array(
'who' => 'authors',

which is not correct, because there are also users with the role "subscriber" which should be displayed.

#2710952

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ahh ok - I've updated the scrip as given under:

add_shortcode("custom-search-author-filter", "func_get_all_authors_belongs_posttype");
function func_get_all_authors_belongs_posttype() {
  

  $out = '<select name="author-filter" class="js-wpv-filter-trigger">';
  $out = $out.'<option value="">Select Author</option>';
  $users = get_users();
  
  $users = get_users(array(
        'has_published_posts' => array('astrofoto'),
     ));
  
  
  $author_filter = '';
  if(isset($_GET['author-filter'])){
      $author_filter = $_GET['author-filter'];
  }
  foreach ($users as $user) {
    $selected = '';
    if($author_filter == $user->ID){
        $selected = ' selected';
    }
    $out = $out.'<option value="' . $user->ID . '"' . $selected . '>' . $user->display_name . '</option>';
  }
  $out = $out."</select>";
  return $out;
}

Can you please confirm now it's working as expected.

#2710953

Thanks, what is the reason, that the list is not sorted correctly by a-z?

#2710963

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've again adjusted the code as given under:

add_shortcode("custom-search-author-filter", "func_get_all_authors_belongs_posttype");
function func_get_all_authors_belongs_posttype() {
  

  $out = '<select name="author-filter" class="js-wpv-filter-trigger">';
  $out = $out.'<option value="">Select Author</option>';
  $users = get_users();
  
  $users = get_users(array(
        'orderby'=>'display_name',
        'order'=>'ASC',
        'has_published_posts' => array('astrofoto'),
     ));
  
  
  $author_filter = '';
  if(isset($_GET['author-filter'])){
      $author_filter = $_GET['author-filter'];
  }
  foreach ($users as $user) {
    $selected = '';
    if($author_filter == $user->ID){
        $selected = ' selected';
    }
    $out = $out.'<option value="' . $user->ID . '"' . $selected . '>' . $user->display_name . '</option>';
  }
  $out = $out."</select>";
  return $out;
}

I hope this is resolved now and I urge you to open a new ticket with every new question you may have. This will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue reported with this ticket.

Thank you for understanding and glad to help.