Skip Navigation

[Resolved] Custom Shortcode, Parametric Search & Views

This support ticket is created 7 years, 5 months 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)

Tagged: 

This topic contains 12 replies, has 3 voices.

Last updated by ioannisM-2 7 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#528206

Hello,

This ticket is for Luo. I am reffering to this ticket: https://toolset.com/forums/topic/how-to-check-if-post-author-is-logged-in/ .

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.

Thank you!

#528370

Minesh
Supporter

Languages: English (English )

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

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"

See the following image:
=> hidden link

More info:
=> https://toolset.com/documentation/user-guides/filtering-views-query-by-author/

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.

#528999

Hello Minesh,

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

Minesh
Supporter

Languages: English (English )

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

The action hook wp_login runs when the user logs in - it can run a simple function.

For example:

function do_anything() {
    //do stuff
}
add_action('wp_login', 'do_anything');

More info:
=> https://codex.wordpress.org/Plugin_API/Action_Reference/wp_login

For - logout I think you can use any filter or shortcode to check user is logout.

#530888

Hello Minesh,

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?

Thank you a lot for your support

#531322

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.

#531672

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.

If you agree, I can setup a demo in your website.

#531785

Hello Luo,

yes please set up the demo in my website. Thank you!

#532606

OK, I am checking it in your website, will feedback if there is anything found.

#532622

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

You can try this:
1) add the PHP codes :

add_filter( 'wpv_filter_query', 'author_online_filter_func', 1, 3 );
  
function author_online_filter_func( $query_args, $view_settings, $view_id ) {
  
    if ( $view_id == 511 && isset($_GET['users_online'][0]) ) {
		$logged_in_users = get_transient('users_online');
		
		$logged_in_users_ids = array();
		foreach($logged_in_users as $k => $v){
			$logged_in_users_ids[] = $k;
		}
	  
	  if($_GET['users_online'][0] == 1){
		$query_args['author__in'] = $logged_in_users_ids;
	  }
	  if($_GET['users_online'][0] == 0){
		$query_args['author__not_in'] = $logged_in_users_ids;
	  }
    }
    return $query_args;
}

2) display online author's posts:
hidden link

Off line author's post:
hidden link

Again, it will be great if you can create new thread for each question, that will help other users to find the anwers

#533079

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.

Thank you again

#533097

As your request, removed the domain names.

#533101

Perfect, thank you Luo!