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?
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
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?
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
Thanks for this. Let me take a shot at this, Can we keep this ticket open for a bit?
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