Skip Navigation

[Resolved] Filtering by favorited post

This support ticket is created 5 years, 8 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/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 5 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1221350

I have installed this plugin:
https://wordpress.org/plugins/favorites/

I have created several views that I would like to be able to filter so that the view only displays custom posts that that the current user has favorited. I see there is a shortcode that allows you to display a list but that has limitations. For example it doesn't work to display posts that are part of nested views, it doesn't allow for sorting, etc.

I've reviewed the plugin's documentation but can't see anything that helps me understand how I might accomplish this. Any ideas?

#1221435

Hi Briana,

Thanks for asking! I'd be happy to help.

If your goal is to only show those posts through a view, which current user has favorited, this will involve some custom programming.

For example, the plugin's introduction page ( https://wordpress.org/plugins/favorites/ ), mentions the function "get_user_favorites" which returns IDs of all favorited posts by a user.

You can use that in a custom shortcode ( https://codex.wordpress.org/Shortcode_API ) which returns true or false, based on whether a specified post ID exists in that favorited posts array or not.

You can then wrap the output of your view's single loop item inside a conditional block so that it is only shown when the shortcode has returned true.
( ref: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/ )

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1221905

Thank you for your guidance. I'm going to take a run at trying to do it. Can you give me any further guidance on how to write the function for the shortcode?

#1222161

Hi Briana,

You can structure your custom shortcode to accept two parameters "user_id" and the "post_id", using the examples from this page:
https://codex.wordpress.org/Shortcode_API

In your active theme's "functions.php" file, you can include the following code to register a custom shortcode "check_if_favorite":


// shortcode to check if a post is favorited or now
function check_if_favorite_func( $atts ) {
	$a = shortcode_atts( array(
		'user_id' => '',
		'post_id' => '',
	), $atts );

	if( (!empty($a['user_id'])) && (!empty($a['post_id'])) )
	{
		$favorited_arr = get_user_favorites($a['user_id']);

		if (in_array($a['post_id'], $favorited_arr))
		{
			return '1';
		}
	}

}
add_shortcode( 'check_if_favorite', 'check_if_favorite_func' );

This will return 1 if a specific post is favorited and nothing otherwise.

After that you'll be able to use the shortcode in the view with the parameters like this:


[check_if_favorite user_id='[wpv-user field="ID"]' post_id='[wpv-post-id]']

Next, to use this shortcode in the conditional display blocks, you'll first need to include "check_if_favorite" in the "Third-party shortcode arguments" section at WP Admin -> Toolset -> Front-end Content.
( screenshot: hidden link )

After that, the shortcode can be used in conditional blocks inside a view, for example:


[wpv-conditional if="( '[check_if_favorite user_id='[wpv-user field="ID"]' post_id='[wpv-post-id]']' ne '' )"]
This is a favorited post!
[/wpv-conditional]

[wpv-conditional if="( '[check_if_favorite user_id='[wpv-user field="ID"]' post_id='[wpv-post-id]']' eq '' )"]
This is not a favorited post!
[/wpv-conditional]

regards,
Waqar

#1223146

Thanks for this. Let me take a shot at this, Can we keep this ticket open for a bit?

#1223851

Hi Briana,

Thanks for writing back.

You can keep the ticket open and normally it will send you a reminder in about a week's time before getting automatically closed. By that time, if you'd still like to keep it open, you can just reply with some text like "please keep open".

regards,
Waqar