Skip Navigation

[Closed] filter by default logged in user

This support ticket is created 3 years, 1 month 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 7 replies, has 2 voices.

Last updated by Luo Yang 3 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2262169
Screenshot 2022-01-10 at 12.05.10 AM.png

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?

#2262537

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

#2265657
Screenshot 2022-01-13 at 10.53.00 PM.png

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;
}
#2266111

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

#2266645

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

#2268885

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

#2272719
Screenshot 2022-01-22 at 9.40.32 AM.png

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;
}
#2273595

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

The topic ‘[Closed] filter by default logged in user’ is closed to new replies.