WordPress.org

Forums

WP Favorite Posts
Display a user's favorites when viewing their profile (19 posts)

  1. jeremyarey
    Member
    Posted 2 years ago #

    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?

    http://wordpress.org/extend/plugins/wp-favorite-posts/

  2. bostondave08
    Member
    Posted 2 years ago #

    Interested in the answer as well.

  3. Khoiruddin
    Member
    Posted 2 years ago #

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

    <?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 :)

  4. cip6791
    Member
    Posted 2 years ago #

    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 ; ?>
  5. solveigm
    Member
    Posted 2 years ago #

    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?

  6. solveigm
    Member
    Posted 2 years ago #

    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?

  7. solveigm
    Member
    Posted 2 years ago #

    Never mind, solved it!

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

  8. Khoiruddin
    Member
    Posted 2 years ago #

    solveigm > You are wellcome :-)

  9. qzha017
    Member
    Posted 2 years ago #

    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

  10. Milchek
    Member
    Posted 2 years ago #

    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!

  11. oomskaap
    Member
    Posted 2 years ago #

    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>";
      } ; ?>
  12. lildragn
    Member
    Posted 2 years ago #

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

    :( Any ideas?

  13. oomskaap
    Member
    Posted 2 years ago #

    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

  14. oomskaap
    Member
    Posted 2 years ago #

    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 }?>
  15. entrosolutions
    Member
    Posted 2 years ago #

    @oomskaap Thanks for your input.

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

    Thx

  16. 1BJK903
    Member
    Posted 1 year ago #

    @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?

  17. MiTa
    Member
    Posted 1 year ago #

    Thank you

    Khoiruddin

    at last i solve this problem by deleting AND $wpdb->usermeta.meta_key = 'wpfp_favorites'. I edited

    Khoiruddin
    's code . Add this code in author.php it will show users's favorite post in users'profile page .
    <?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->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>';
    }
    ?>
  18. 1BJK903
    Member
    Posted 1 year ago #

    Thanks mate, but this one doesn't show the favorite posts of the admin... Why is that?

  19. 1BJK903
    Member
    Posted 1 year ago #

    PS: Oomkaap's code shows only the one of the admin... The rest of the users' favorite posts are invisible.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP Favorite Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic