add_action('wp', 'update_online_users_status');
function update_online_users_status(){
if(is_user_logged_in()){
// get the online users list
if(($logged_in_users = get_transient('users_online')) === false) $logged_in_users = array();
$current_user = wp_get_current_user();
$current_user = $current_user->ID;
$current_time = current_time('timestamp');
if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
$logged_in_users[$current_user] = $current_time;
set_transient('users_online', $logged_in_users, 30 * 60);
}
}
}
add_shortcode('author_is_user_logged_in', 'author_is_user_logged_in');
function author_is_user_logged_in($user_id) {
if(!$user_id){
$user_id = get_the_author_meta( 'ID' );
}
// get the online users list
$logged_in_users = get_transient('users_online');
$res = 0;
// online, if (s)he is in the list and last activity was less than 15 minutes ago
if(isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] > (current_time('timestamp') - (15 * 60)))){
$res= 1;
};
return $res;
}
You helped me create a shortcode that shows if the post author is logged in. How could I use the shortcode's value to
1) Show in a view all the "provider" posts of the logged in authors.
2) Add a filter in the post type's parametric search so the visitors can choose to see posts of the authors that are logged in?
Could it be possible instead of updating the transient updating the post field "wpcf-my-status" of the post type "provider"? This way it will be easier to use that info in views sorting or parametric search.
Hello. Thank you for contacting the Toolset support.
Luo is on vacation and this is Minesh here and I'll try to help you with your issue. Hope this is OK.
1) Show in a view all the "provider" posts of the logged in authors.
- Yes - you can filter post by author and add query filter for author and select option "post author is same as logged in user"
2) Add a filter in the post type's parametric search so the visitors can choose to see posts of the authors that are logged in?
I think this will be achieved with the same solution by adding author filter.
If I misunderstood your requirement, please share few screenshot and problem URL.
I need to show in the view only posts of the logged in users not to themselves but to everybody. If I choose the post author to be the same as the logged in user in the query filter, then only the authors will see their own posts.
I think that the only way to make it work is if I create a post field that would be updated to 1 each time the author logged in and to 0 each time the author logged out. Then I could use this field in parametric search, to sort posts and also in post query filters.
So would it be possible to combine the following codes in order to achieve that?
This code creates a database transient each time the user is active or non active
add_action('wp', 'update_online_users_status');
function update_online_users_status(){
if(is_user_logged_in()){
// get the online users list
if(($logged_in_users = get_transient('users_online')) === false) $logged_in_users = array();
$current_user = wp_get_current_user();
$current_user = $current_user->ID;
$current_time = current_time('timestamp');
if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
$logged_in_users[$current_user] = $current_time;
set_transient('users_online', $logged_in_users, 30 * 60);
}
}
}
add_shortcode('author_is_user_logged_in', 'author_is_user_logged_in');
function author_is_user_logged_in($user_id) {
if(!$user_id){
$user_id = get_the_author_meta( 'ID' );
}
// get the online users list
$logged_in_users = get_transient('users_online');
$res = 0;
// online, if (s)he is in the list and last activity was less than 15 minutes ago
if(isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] > (current_time('timestamp') - (15 * 60)))){
$res= 1;
};
return $res;
}
Instead of a transient been created, is it possible to the post field 'wpcf-my-status' been updated, each time the user is logged in or logged out?
add_filter('my_custom_actions', 'my_custom_post_actions', 10, 3);
function my_custom_post_actions ($custom_actions, $post_id, $form_data) {
// if the post is of type 'provider'
$postType = get_post_type( $post_id );
if ( $postType == 'provider' )
{
// set the value to 1
$custom_actions[] = array( 'meta_key' => 'wpcf-my-status', 'meta_value' =>'1' );
}
return $custom_actions;
}
I am not developer so it's very difficult for me to make it work. This ticket is the second part of another ticket that Luo helped me and is based on his custom solution https://toolset.com/forums/topic/how-to-check-if-post-author-is-logged-in/ As Luo has now retutned from his vacation could it be possible to assign this ticket to him?
Since it is a custom PHP codes problem, please provide a test site with same problem, also point out the problem URL and View URL, and where I can edit your PHP codes.
It is recommended to store author online status into a custom post field, for example:
When a author is logged in, you will need to update all custom post fields value, it will conduct performance problem, so I suggest you persists on transient, which needs only to update the wordpress cache.
Since your website isn't in English and very slow, it takes time to test and debug in your website
Q1) ) Show in a view all the "provider" posts of the logged in authors.
You can try this:
1) Modify the PHP codes to:
add_shortcode('author_is_user_logged_in', 'author_is_user_logged_in');
function author_is_user_logged_in($atts) {
$atts = extract(shortcode_atts(
array(
'user_id' => '',
), $atts));
if(!$user_id){
$user_id = get_the_author_meta( 'ID' );
}
// get the online users list
$logged_in_users = get_transient('users_online');
$res = 0;
// online, if (s)he is in the list and last activity was less than 15 minutes ago
if(isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] > (current_time('timestamp') - (15 * 60)))){
$res= 1;
};
return $res;
}
2) Use above shortcode in Views loop section, like this:
[wpv-post-author], online status: [author_is_user_logged_in user_id="[wpv-post-author format="meta" meta="ID"]"]
Q2) Add a filter in the post type's parametric search so the visitors can choose to see posts of the authors that are logged in
Luo thank you so much for your support, you are awesome!!! Could it be possible to remove my domain from your previous reply and after that I can mark this ticket as resolved.