Dear Sir/Madam,
I want to build one View to filter the post by author id, if no author id is provided, author id is the current logged in user, it is possible to do?
Hello,
It is possible with custom codes, for example:
add_filter( 'wpv_filter_query', 'default_author_filter', 99, 3 );
function default_author_filter( $query_args, $view_settings, $views_id ) {
if ( $views_id == 123 ) { // specific view ID
if(!isset($query_args['author'])){
$query_args['author'] = 456; // specific user ID
}
}
return $query_args;
}
More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Dear Luo Yang,
Thanks for your reply but it doesn't work. The error prompted "This is a word reserved by WordPress" if I set the URL parameter as author
I want to show the view with the current login author rather than a specific user. I try to change your script as below but doesn't work, it show all post without any filter.
add_filter( 'wpv_filter_query', 'default_author_filter', 99, 3 );
function default_author_filter( $query_args, $view_settings, $views_id ) {
if ( $views_id == 3583 ) { // specific view ID
if(!isset($query_args['authorid'])){
$query_args['authorid'] = get_current_user_id(); // current user ID
}
}
return $query_args;
}
Since it is a custom codes problem, please provide a test site with the same problem, also point out the problem page URL and view URL, where I can edit your custom PHP codes, I need a live website to test and debug, thanks
Dear Luo Yang,
Due to the company policy, the production site is not allowed to have external user access, do you mind having a discover site from toolset so that I can prepare some data for testing?
Best regards,
Kelvin
The custom codes I mentioned above works fine in my localhost, you can provide a test site with the same problem, fill below private message box with website credentials, I can setup a demo for you
Dear Luo Yang,
I tested, it works if I pass the authorid but not be triggered if without authorid, I added the command exit to script, it doesn't end the script
add_filter( 'wpv_filter_query', 'default_author_filter', 99, 3 );
function default_author_filter( $query_args, $view_settings, $views_id ) {
if ( $views_id == 3583 ) { // specific view ID
exit;
if(!isset($query_args['authorid'])){
$query_args['authorid'] = get_current_user_id(); // specific user ID
}
}
return $query_args;
}
As I mentioned above, it works fine in my localhost, and the problem you mentioned above is abnormal, if you need more assistance for it, you can provide a test site with the same problem, fill below private message box with website credentials, I can setup a demo for you
Private message box enabled again