Skip to content

Instantly share code, notes, and snippets.

Last active October 14, 2022 19:33
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylephillips/9fe12f195c671c989af3 to your computer and use it in GitHub Desktop.
Save kylephillips/9fe12f195c671c989af3 to your computer and use it in GitHub Desktop.
Using the Favorites get_user_favorites() function to display favorited post data in a custom format
// Method 1: simple foreach loop
$favorites = get_user_favorites();
if ( isset($favorites) && !empty($favorites) ) :
foreach ( $favorites as $favorite ) :
// You'll have access to the post ID in this foreach loop, so you can use WP functions like get_the_title($favorite);
* Method 2: WP_Query Object
* Check if there are favorites before instantiating the WP_Query.
* Passing an empty array as an argument returns ALL posts.
* @see
$favorites = get_user_favorites();
if ( $favorites ) : // This is important: if an empty array is passed into the WP_Query parameters, all posts will be returned
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // If you want to include pagination
$favorites_query = new WP_Query(array(
'post_type' => 'post', // If you have multiple post types, pass an array
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'post__in' => $favorites,
'paged' => $paged // If you want to include pagination, and have a specific posts_per_page set
if ( $favorites_query->have_posts() ) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
// Treat this like any other WP loop
next_posts_link( 'Older Favorites', $favorites_query->max_num_pages );
previous_posts_link( 'Newer Favorites' );
endif; wp_reset_postdata();
else :
// No Favorites
Copy link

iecoding commented May 5, 2015


Copy link

This is super helpful. Thank you!

Copy link

I'm trying to use the second method. I have a custom post type "villa" I tried to change the 'post_type' => 'post' to 'post_type' => 'villa' but unfortunately nothing happened. No results at all are shown on the favorites page. Can you please advise me to the right direction. Thank you!

Copy link

Hey , please tell me where to put this code or file.

Copy link

jruca commented Mar 11, 2016

I am trying to use this in conjunction with the JS callback favorites_after_button_submit to remove an <li> item from the custom favorites list when it is un-favorited (the way the default favorites list works), but it seems the callback only provides the current list of favorites and not which <li> was acted on. Is there some other way to achieve this functionality?

Copy link

Hi, How can I add the thumbnail of the post?

Copy link

pgroot91 commented Aug 21, 2016

Thanks for the awesome code and ofcourse the plugin this is very usefull! Keep up the great work..

Copy link

Anyone managed to instead of clearing all favorites, clear by choice?

Of course this can be done by clicking on the fav btn on post again, but would be convenient to do it within the saved favs page.

Copy link

prem025 commented Sep 20, 2016

instead of clearing all favorites, clear by choice? help someone

Copy link

Hello, nice plugin and very helpful function. One thing is that the query won't display private posts as favorites, even with 'post_status' => 'any'. Any ideas ?

Copy link

Hello all! I am "tripling up" on 2 of the previous comments. Been working on my custom display for the last couple of days and can't get the favorites button to remove posts dynamically one-by-one like it does when you output the list with the shortcode. Any hints on how to call these functions within my custom query? If this just isn't possible yet, please let me know - I have a tendency to keep trying things until I figure it out :)

Copy link

Let me know who!
Where do I put the above code, and let me know how to use it?
Please give me a little tip. Somebody ...

Copy link

This plugin is great....

Copy link

ProWeb-Dev commented Jan 21, 2017

To put the option to clean, without removing all, you just call the_favorites_button button ($ post_id, $ site_id) Inside the loop on the page that lists all the user's favorites logged in or anonymous.
You can change the text of the favorited button to remove favorite.
This worked for me.
I hope this tip is useful, check out my English is not very good.

Copy link

Great plugin, and very useful code snippet. Thank you!

Copy link

thadbloom commented Mar 16, 2017

This code no longer seems to work for anonymous users. When I'm logged into the admin, it works fine.

As an anonymous user, my favorites do not appear in the custom query.

Edit: discovered that this code is not working because the server is running PHP 7- I tested with 5.4.4 and it still works.

Copy link

This is awesome thank you!

Copy link

For those who wants to avoid loop from repeating in category template use

if ( $favorites_query->have_posts() ) : $favorites_query->the_post(); and remove endwhile;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment