Hello,
I would like to add Author in View Search Filter. Please let me know, how can I do that?
Hi,
Thank you for contacting us and I'd be happy to assist.
The post author search field is something that is not supported out of the box, so this will require some workaround:
1. You'll first register a custom shortcode, that can create a select/drop-down field for the post authors.
add_shortcode("list-of-authors", "list_of_authors");
function list_of_authors() {
$out = '<select name="author-filter" class="js-wpv-filter-trigger form-control"><option value="">-select-</option>';
$users = get_users();
$selected = "";
if(isset($_GET['author-filter'])){
$selected = $_GET['author-filter'];
}
foreach ($users as $user) {
$out .= '<option value="' . $user->ID . '" ' . selected( $selected, $user->ID, false ) . '>' . $user->display_name . '</option>';
}
$out .= '</select>';
return $out;
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
2. Next, you can include this shortcode through a "Fields and Text" block, within your view's "View Search" block, where you'd like to show this author search field:
( example screenshot attached: screenshot-post-author-search.png )
3. The last step would be to add a 'post author' query filter in the view's 'Content Selection' section so that it can be linked to the post author ID passed through the URL parameter 'author-filter', coming from the post author search field.
( example screenshot attached: screenshot-post-author-query-filter.png )
I hope this helps and please let me know if you need any further assistance with this.
regards,
Waqar
Thank you for the solution. However, "Fields and Text" currently has an issue with WordPress 6.1 and downgrade is not my option. Could you please let me know if there are other solutions without "Fields and Text", or I have to wait for "Fields and Text" issue has been fixed.
Thanks for writing back.
You don't necessarily have to use the "Fields and Text" block to make this shortcode work. As an alternative, you can use the "Shortcode" block or the simple "Custom HTML" block.
My issue is resolved now. Thank you!