Skip Navigation

[Resolved] How to query filter by User ID from a string with multiple values

This thread is resolved. Here is a description of the problem and solution.

Problem:

Create a custom shortcode to check if current user's ID is added into that custom feild, and use the shortcode as parameter of [wpv-conditional].

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/how-to-query-filter-by-user-id-from-a-string-with-multiple-values/#post-1605019

Relevant Documentation:

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

Last updated by toddU 3 years, 12 months ago.

Assisted by: Luo Yang.

Author
Posts
#1600993

I am trying to show a view that is filtered by a custom field (via 3rd party plugin) and the current user.

The custom field is: _wppcp_post_page_allowed_users
CPT: Trip
On each "Trip", I add multiple users to the custom field. The custom field stores the user IDs in a string, like "22, 25, 67, 84".

What I want to do: On a given page, with the view "Past Trips Current User" showing...the wpv-post-link should show for each "Trip" that the logged in user has been added to the custom field. So if there are 50 "Trips" in the database, but the logged in user has been added to the custom field for only two of them...then only those two should show in the view.

PARTIAL SUCCESS
I have managed to get this to work by using wpv-conditional logic in the template for the view. However, it ONLY works for the FIRST user ID in the string. If the user IDs in the custom field are "22, 25, 67, 84" --- then it works successfully for user 22. BUT, it does not work for 25, 67, 84. I get nothing returned.

I believe I need to find a way to compare or query based on the custom field "containing" the user ID, not eq or ne to. But I don't know how to do that.

I am also concerned about future larger numbers. Meaning, if the user added to the custom field is 25...I need to make sure that users 125, 225, 325, etc. do not see the trip.

Any suggestions on how to accomplish this?

#1602207

Hello,

You can create a custom shortcode to check if current user's ID is added into that custom feild, and use the shortcode as parameter of [wpv-conditional], for example:
1) Create a custom shortcode [field_contains_userid], add below codes into your theme file "functions.php":

add_shortcode('field_contains_userid', function($atts, $content){
	$atts = shortcode_atts( array(
		'field' => '_wppcp_post_page_allowed_users',
		'user_id' => get_current_user_id(),
	), $atts );
	$res = 0;
	$str = get_post_meta(get_the_ID(), $atts['field'], true);
	if($str){
		$arr = explode(" ,", $str);
		if(in_array($atts['user_id'], $arr)){
			$res = 1;
		}
	}
	return $res;
});

2) Dashboard-> Toolset-> Settings-> Front-end Content, in section "Third-party shortcode arguments", add the shortcode name: field_contains_userid

3) Use above shortcode like this:
[wpv-conditional if="([field_contains_userid] eq 1)"]
logged in user has been added to the custom field
[/wpv-conditional]

More help:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/

#1602755

This doesn't work. I don't see anything in the view, even if the current user is the only one in the field.

Something is wrong with the function to create the shortcode. I am getting a 0 when I should be getting a 1.

#1603447

I have tried it in my localhost, it works fine, if you need more assistance for it, please provide a test site with the same problem, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug, thanks

#1605019

Thanks for the details, I have edit the PHP codes as below:

add_shortcode('field_contains_userid', function($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '_wppcp_post_page_allowed_users',
        'user_id' => get_current_user_id(),
    ), $atts );
    $res = 0;
    $arr = get_post_meta(get_the_ID(), $atts['field'], true);
	
        if(in_array($atts['user_id'], $arr)){
            $res = 1;
        }
    return $res;
});

Please test again, check if it is fixed.

#1605649

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.