Skip Navigation

[Resolved] Conditional display posts based on custom user meta

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

Problem:

Use Toolset Views to query current user's posts setup by "Favorites" plugin.

Solution:

You can create a custom shortcode, and get all post IDs setup by "Favorites" plugin, then pass it to view, for example:

https://toolset.com/forums/topic/conditional-display-posts-based-on-custom-user-meta/#post-1735553

Relevant Documentation:

https://toolset.com/documentation/user-guides/views/filtering-views-query-by-post-id/

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

This topic contains 6 replies, has 2 voices.

Last updated by Dan Kitsmiller 4 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1735225

I've created a view that displays a list of posts from the "Homes" custom post type. I also have a custom user meta field called "Favorites" that contains a list of post IDs that the user has marked as a "favorite".

I'm trying to add conditional logic to the loop out put of the view, so that displays a list of the current user's "favorite" posts.

I need to create a conditional argument that compares the post ID of the current loop item against the post IDs saved in the current user's meta. So only if the "Home" post ID is present in the user's "Favorites" metadata, the loop item is displayed.

I've made several attempts to evaluate the conditions, but I'm unsure how to compare against a multi-value user meta field like this. Thanks for your help... your support team is outstanding 🙂

#1735387

Hello,

How do you setup the user field "Favorites"?

If it is a multiple instances field, you can try these:
1) Create a post view:
- Query "Home" posts
- Filter by:
Include only posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]
- In view's loop, display the home post information

2) Display above post view shortcode, and pass user field "Favorites" value as shortcode attribute, like this:

[wpv-view name="your-view-slug" ids="[types usermeta='favorites' output='raw' separator=', ' current_user='true'][/types]"][types usermeta='favorites' format='FIELD_VALUE' separator=', ' user_is_author='true'][/types]

Please replace "your-view-slug" with your view slug of step 1, replace "favorites" with your custom user field slug

More help:
https://toolset.com/documentation/user-guides/views/filtering-views-query-by-post-id/

#1735435

Hi Lou, thanks for your speedy reply. I tried your suggestion, but it returns the following error:
Notice: Array to string conversion in /var/www/wp-content/plugins/types/application/models/view/decorator/separator.php on line 22

The code you sent in your last reply looks like it actually contains an extra shortcode at the end. I tried the following two variations:

Version 1:

[wpv-view name="test-favorites-view" ids="[types usermeta='simplefavorites' output='raw' separator=', ' current_user='true'][/types]"]

Version 2:

[wpv-view name="test-favorites-view" ids="[types usermeta='simplefavorites' format='FIELD_VALUE' separator=', ' user_is_author='true'][/types]"]

I think that the "array to string" conversion error may be due to the fact that the user meta field contains some text in addition to the post IDs. The field was created by the "Favorites" plugin by Kyle Phillips: hidden link

Here is a sample of the meta field contents for a user:
1, 2854, 281, 731, 447, 467, 1, 1, Default List, 2854, 281, 731, 447, 467

I'm not sure what some of these values, or the term "Default List" represent, but the values after the words "Default List" (2854, 281, 731, 447, 467) are the correct post IDs of the user favorites. This is consistent for every user.

Thanks again for your help!!

#1735451

How do you setup the custom user field "favorites"?
Please take a screenshot for it, I need to test it in my localhost, thanks

#1735459

The custom user field was created by the "Favorites" plugin I mentioned. I exported a CSV file of my Users database, populated with sample data for you here: hidden link

The serialized data you are looking for is in the "simplefavorites" column.

#1735553

Your CSV file does not help.

I have tried the plugin "Favorites" download from:
https://wordpress.org/plugins/favorites/

You can try these:
1) In your theme file, functions.php, add below codes:

add_shortcode( 'my-fav-ids', function($atts){
	$arr = array();
	if(function_exists('get_user_favorites')){
		$arr = get_user_favorites(get_current_user_id(), $site_id = null, $filters = null);
	}
	$res = '';
	if($arr){
		$res = implode(',', $arr);
	}
	return $res;
});

This will create a shortcode [my-fav-ids]

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

3) Use above shortcode like this:
[wpv-view name="test-favorites-view" ids="[my-fav-ids]"]

#1735593

That worked perfectly, and is a much better solution. Thank you for helping me so quickly!