Home › Toolset Professional Support › [Resolved] How to create a list to display the last 5 CPT posts created
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 |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
Tagged: Content Templates, Views, Views plugin, WordPress Archives
Related documentation:
This topic contains 45 replies, has 2 voices.
Last updated by Nigel 5 years, 9 months ago.
Assisted by: Nigel.
Hi Nigel,
do you know if it possible display the favorite posts list using a View?
Hi Nigel,
I have found the solution, using the custom field generated by the Favorites plugin.
But now, I need to display the Profile posts about the users that have added my profile post into them favorites list. What should I do?
Thanks
Hi Nigel,
I don't know why, I have done the access with another account to test the add to favorites, and now something went wrong, in the favorites page are displayed all profile posts, and this also for other accounts...
(see the attached images)
What is the issue and what should I do to solve that?
Than,
I have added this code in the loop
[wpv-post-field name='simplefavorites_count']
to display the value content in the fields. And so, in the simplefavorites_count custom field are saved how many times the single post is added to favorites by every user... And then the value is not related to the current user selection (0 or 1)...
I don't know what should I do to solve it...
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
I'll need to take some time to study the plugin to see how it works before I can give you any answers.
I have quite a few tickets in my queue to work through, but I'll get back to you as soon as possible.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Francesco
I took a quick look at how Favorites stores its data.
In wp_postmeta it simply stores a running total of how many people have liked a post.
The real data is stored in wp_usermeta, where it stores a list of all the posts liked by a user.
That data is buried in an array, like so:
Array ( [0] => Array ( [site_id] => 1 [posts] => Array ( [0] => 124 [1] => 104 ) [groups] => Array ( [0] => Array ( [group_id] => 1 [site_id] => 1 [group_name] => Default List [posts] => Array ( [0] => 124 [1] => 104 ) ) ) ) )
Helpfully, though, the plugin provides a shortcode "post_favorites" which can be used to generate a list of all the users who have liked a post. (See the shortcodes section of the documentation: hidden link.)
So you could use that shortcode as an attribute in a wpv-view shortcode that queries users, for example.
Hi Nigel,
sorry for my delay in reply, these days I was out of my city and I didn't have a computer to do some tests...
So, I have tried to use the "post_favorites" shortcode as an attribute in a wpv-view shortcode that queries users, but I don't know the correct logic and syntax...
Then, I have done that:
1 - In the front-end content section I have added the "post_favorites" in third-party shortcode arguments;
2 - I have created a View with Selected Content by "Users" with any rules
3 - and with the Query Filter like this "Seleziona gli utenti con id impostato dall'attributo dello shortcode della vista "users" e con il ruolo di "(any)" es.: [wpv-view name="view-name" users="1"]"
4 - and in the loop, I have added the single item Template content.
5 - I have created a View with Selected Content by Porfili Post Type;
6 - with the query filter to display only the published posts and 5 posts per page
7 - and in the loop I have called the View in point 2 with this shortcode: [wpv-view name="lista-favoriti-filtrato-per-campo-utente" users="post_favorites"] and the pagination code.
So, on the page where I display the favorites posts, I have the pagination and 5 "No items found"...
I don't know what I should do to have what I need... sigh!!!!
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Francesco
Sorry, I haven't been working in support for most of today and I couldn't look at this. I'm back to normal tomorrow and will review it then.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Francesco
I looked into the Favorites plugin code a little and unfortunately it seems to be doing things wrong.
Shortcodes should always return a string (https://developer.wordpress.org/plugins/shortcodes/).
But the post_favorites shortcode we want to use doesn't appear to, it looks like it echoes the results instead. And that's wrong.
So I prepared a custom shortcode which returns a comma-separated list of user-ids that have liked a post, which is what we need.
Could you register this "favorited-by" shortcode and try using that instead of the plugin shortcode?
add_shortcode('favorited-by', function () { global $post; $users = get_users_who_favorited_post($post->ID); $user_ids = wp_list_pluck( $users, 'ID', null ); return implode( ",", $user_ids ); });
Hi Nigel,
I have added your shortcode code, I have saved and activated it. I have added it in the third-party arguments shortcode section.
In the Second View loop, I have modified the shortcode with this:
[wpv-view name="lista-favoriti-filtrato-per-campo-utente" users="favorited-by"]
The displayed results remain the same: the pagination with 5 "no results"...
So, I have tried to insert the [favorited-by] shortcode outside of the wpv-view in the loop and I can see the id of the users who have favorited the single Profile CPT post.
Then, I have edited the wpv-view in the loop with this shortcode:
[wpv-view name="lista-favoriti-filtrato-per-campo-utente" users="[favorited-by]"]
Now I display the pagination, in the first page the list with 4 profile added to favorites and one "No items Found".
Now I need to filter the profiles of the users who have added my profile into their favorites list. I'm trying to set the Query Filter but I think that the option about the current user doesn't work fine here... or I'm setting something wrong.
see the attached image
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Can you remind me of the context, because it is a long time since I looked at this.
We have users that are authors of their own profile posts.
When a user looks at other user profiles they might favorite them.
Now you are working on a View that will display who has liked the profile being looked at. Is that right?
(You mention current user, so you may have an additional step where you create the context of the profile of the current user as the starting point, but the key is that we are starting from a profile.)
I did a quick bit of testing, and this worked for me. If you need to do something differently them please elaborate so I can see how the requirements differ.
So, in my content template for posts (in your case profile posts), I insert a View, and this View will have a Query Filter for the post author where the author(s) are specified by a shortcode attribute "author".
So the shortcode to insert my View it looks like this:
[wpv-view name="posts-authored-by-specified-users" author="[favorited-by]"]
The View itself is ordinary, it just includes the post author filter you can see in the screenshot.
So when I visit a post on the front-end, I can see the posts authored by users who have favorited this post.
I think that covers your scenario, but tell me if not.
Hi Nigel,
Now you are working on a View that will display who has liked the profile being looked at. Is that right?
(I use the first person to do an example)
1 - Now I'm working on a View that will display the list of the profiles where I put the "like".
2 - After I need to work on a View that will display the list of the profiles of the authors who has put "like" on my profile.
So when I visit a post on the front-end, I can see the posts authored by users who have favorited this post.
I think that covers your scenario, but tell me if not.
It's not exactly right. I need to display in two different Pages (not in Profile CPT posts), what I have explained in points 1 and 2 above.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
OK, well 2. is rather different than what I was directing you towards.
If you want to display the list of posts that I have liked then you need to write a shortcode that uses the get_user_favorites function and returns the resulting array of post IDs as a comma-separated list, which you can then pass as a shortcode attribute to your View that displays the favorited profile posts to provide the values for a query filter by post ID.
If you are showing the profiles that I have favorited, where "I" am the current user browsing the site, then that function will return the correct posts.
If you are displaying a profile post and you want to show the profile posts which have been favorited by that user (rather than the current user) then you would need to pass the ID of the author of that profile post to your custom shortcode to set the user_id in the first argument of the get_user_favorites function.
The hidden link documentation doesn't contain anchor links for me to link directly to the relevant part, but you need to scroll down to the Functions section and locate the User Favorite functions.
The implementation is similar but different to the custom shortcode I suggested a few replies back, hopefully you can use that as the basis for writing the custom shortcode you would need that I just described.