• I want to be able to display a user’s favorite posts when anyone views their profile. What code can I use to do this?

    I was thinking that maybe I can do something like:

    <?php wpfp_list_favorite_posts(); ?>

    but that only displays your favorites, and not the favorites of an individual user.

    I saw a thread that says you can use:

    wpfp_get_users_favorites(String userlogin)

    But that just gives you an array of the user’s favorite posts apparently. How do you actually display a person’s favorite posts?

    https://www.ads-software.com/extend/plugins/wp-favorite-posts/

Viewing 15 replies - 1 through 15 (of 18 total)
  • Interested in the answer as well.

    Try my crazy script inside author.php on your theme ?? :

    <?php
    	$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
    	global $current_user;
        get_currentuserinfo();
    	$meta_query = mysql_query("
    		SELECT distinct($wpdb->usermeta.meta_value) FROM
    		$wpdb->posts,  $wpdb->postmeta, $wpdb->users, $wpdb->usermeta
    		WHERE $wpdb->postmeta.post_id = $wpdb->posts.id
    		AND $wpdb->usermeta.user_id = $wpdb->users.id
    		AND $wpdb->postmeta.meta_key = 'wpfp_favorites'
    		AND $wpdb->usermeta.meta_key = 'wpfp_favorites'
    		AND $wpdb->users.id = '".$curauth->ID."'");
    
    	while($array_meta = mysql_fetch_array($meta_query))
    	{
    		$meta = explode('"',$array_meta['meta_value']);
    	}
    
    	class IndexFilter extends FilterIterator {
    		public function __construct (array $data) {
    			parent::__construct(new ArrayIterator($data));
    		}   
    
    		public function accept () {
    			return ($this->key() % 2);
    		}
    	}
    
    	$arr_meta	= $meta;
    	$posts_id = array();
    
    	foreach (new IndexFilter($arr_meta) as $key => $value) {
        $posts_id[$key] = $value;
    }
    ?>
    <h2>Favorite Posts</h2>
    <ul>
    <?php
    $data_posts_id = array_reverse($posts_id);
    foreach($data_posts_id as $show_posts_id){
    	echo '<li><a href="'.get_permalink($show_posts_id).'" title="Permanent link to '.get_the_title($show_posts_id).'">'.get_the_title($show_posts_id).'</a></li>';
    }
    ?>

    good luck ??

    This is how I get a user’s favorite posts with thumbnail in author.php:

    <?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
    
    <?php $user_favs = wpfp_get_users_favorites($curauth->display_name); ?>
    <ul>
    <?php foreach ($user_favs as $user_fav) :
    echo "<li><a href='".get_permalink($user_fav)."' title='". get_the_title($user_fav) ."'>" . get_the_post_thumbnail( $user_fav, array(335,335) ) . "</a></li>";
    endforeach ; ?>

    Hi!

    I am in desperate need of help! I tried pasting cip6791s code in my author.php-file, deleted it again as it didn’t quite look the way I wanted, and somehow, now some sort of “post template” for the favourite page has showed up and refuses to go away. I have tried replacing my author-php-file with the original file from the theme, but it’s still there!

    I’m guessing it’s retrieving some command from another file, but I can’t figure out which one it is. Any suggestions / solutions?

    Hi!

    I am in desperate need of help! I tried pasting cip6791s code in my author.php-file, deleted it again as it didn’t quite look the way I wanted, and somehow, now some sort of “post template” for the favourite page has showed up and refuses to go away. I have tried replacing my author-php-file with the original file from the theme, but it’s still there!

    Looks like this

    I’m guessing it’s retrieving some command from another file, but I can’t figure out which one it is. Any suggestions / solutions?

    Never mind, solved it!

    And btw, Khoiruddin, your code worked perfectly! Thank you!!

    solveigm > You are wellcome ??

    qzha017

    (@qzha017)

    Hi Khoiruddin I tried your code it returned error.

    I need to display current user’s favourite posts in the profile page, I am able to create a page say “favourites” and link it in the profile page, but I want to show the actual content of that favourites page listed on my profile page not a favourites link to the lists.

    Can you please help me out?
    Thanks in advance

    This is a simpler way by just getting the users meta:

    $favorite_post_ids = get_usermeta($user_id, 'wpfp_favorites');
    include("wpfp-page-template.php");

    So obviously you make a copy of the wpfp-page-template.php and put it in your themes folder!

    The above code will throw out errors if a user does not have any favorites.

    The correct code is

    <?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
    
    <?php $user_favs = wpfp_get_users_favorites($curauth->display_name); ?>
    <ul>
    
    <?php
    foreach ($user_favs as $user_fav)
      {
    echo "<a href='".get_permalink($user_fav)."' title='". get_the_title($user_fav) ."'>" . get_the_post_thumbnail( $user_fav, array(335,335) ) . "</a>";
      } ; ?>

    Unfortunately I’m still getting an error with your code oomskaap
    Warning: Invalid argument supplied for foreach() in

    ?? Any ideas?

    Yes i also get that error, but only if you have NO favorites. Once you added one, it works. I’m also still trying to find a solution

    Working code:

    <?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
    
    <?php $user_favs = wpfp_get_users_favorites($curauth->display_name); ?>
    <?php
    if(!$user_favs) { echo "No favorites yet."; } else{
    	?>
    
    <ul><?php
    foreach ($user_favs as $user_fav)
      {
    echo "<a href='".get_permalink($user_fav)."' title='". get_the_title($user_fav) ."'>" . get_the_post_thumbnail( $user_fav, array(335,335) ) . "</a>";
      };?></ul>
    <?php }?>

    @oomskaap Thanks for your input.

    Could you tell me how to display the same but in my own loop ?

    Thx

    @oomskaap:

    It does not work. It shows only a “no favorites yet”, but you can’t see favorites if a user does..

    Could you help me with it please?

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘[Plugin: WP Favorite Posts] Display a user's favorites when viewing their profile’ is closed to new replies.