I am trying to show favourite posts for a user. I am using hidden link to add a fav post to the user. I was able to set the view that filter through the ids passed for the current logged in user, but when none of the posts is marked fav, "" is passed into the view and all of the posts that are on the website are shown?
How can I show nothing when when no id is passed so that I can write no posts added to fav. Attaching the filter for my view. hidden link
If you somehow managed to pass the value of "favourites" to the View ShortCode attribute "Ids", then you can also check on that value with an HTML conditional.
I assume you have some ShortCode or function that helps you pass the favourites to the IDs attribute, yes?
If so, please try to register that ShortCode in Toolset > Settings > Front End content > third party shortcode arguments, and then you can try to use it like so in an HTML conditional:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-shortcodes-in-conditions/
[wpv-conditional if="( '[YOUR_SHORTCODE_RETURNING_IDs]' eq '' )"]NO IDs are passed, so, do NOT display the view at all[/wpv-conditional]
[wpv-conditional if="( '[YOUR_SHORTCODE_RETURNING_IDs]' ne '' )"]IDs are indeed passed, so, DO display the view[/wpv-conditional]
Does this help?
If not, I will need to know how precisely you get that value (Ids), so we can check how to determine if there are some ids returned or not.
Hey Beda,
As a result of the above solution, I am able to display all the favorite posts for a user or No post (when there is no favorite marked by user).
In my case, I have two Custom Post Types named as 'A' and 'B'. I want to display the favorite posts of 'A' and 'B' on a separate pages.
By using the given below Shortcode I am getting all the id's of Favorited posts. I want to filter the id's for particular Custom post type.
Suppose if user marked two posts as favorite of post type 'A' and one post of post type 'B' and I created a page [named as "Display Favorite Posts"] where I am displaying all Favorited posts of 'A' , then what is expected is to get id's of only that two post on Display Favorite Posts page.
But I'm getting all three id's there.
I tried to change the value of $filters attribute to the name of that particular custom post type. But it doesn't work.
function custom_favorites_list_thumbnail()
{
$fav = get_user_favorites($user_id = null, $site_id = null, $filters = null);
if($fav){
$ids = implode(', ', $fav);
}
else {
$ids = "";
}
return $ids;
}
add_shortcode('favorite_post_id', 'custom_favorites_list_thumbnail');
This is a question that you could direct to the makers of that plugin since feature and data are created and stored by that plugin.
get_user_favorites() method used here is clearly an API of the plugin you use, not Toolset.