I would advise you not to use the user meta. Although your problem is caused by:
update_post_meta( $author_id, "_user_followed", $followed_USERS ); // Add user ID to author meta
update_post_meta( $author_id, "_author_follow_count", ++$author_follow_count );
note that you called update_post_meta not update_user_meta, so somewhere, there’s a post whose ID is the same as the user ID of the author, that’s getting strange post meta added.
However, it’s still an inefficient method, and prone to race conditions that might give inaccurate counts. You’re also going to have a very costly piece of code when trying to figure out who follows an author because you’ll have to check each and every single user to see if they follow that author.
Instead, use a custom taxonomy.
Using a Custom User Taxonomy
Taxonomies aren’t just for posts. In this taxonomy, terms are authors, and the objects being tagged are users ( specifically we’re using User IDs as our slugs ).
Assuming we call this taxonomy wpse_180398_followers
, to follow an author:
// follow the user, note the last argument is true
$term_ids = wp_set_object_terms( $user_id, $author_id, 'wpse_180398_followers', true );
if ( is_wp_error( $term_ids ) ) {
// There was an error somewhere and the terms couldn't be set.
} else {
// Success!
}
To unfollow someone:
wp_remove_object_terms( $user_id, $author_id, 'wpse_180398_followers' );
To check if a user is following an author:
if ( has_term( $author_id, 'wpse_180398_followers', $user_id ) ) {
// user_id is following author_id!
}
To get the followers of an author:
$followers = get_objects_in_term( $author_id, 'wpse_180398_followers' );
To get an authors follower count:
count( $followers );
To get the authors a user follows:
$author_terms = wp_get_object_terms( $user_id, 'wpse_180398_followers' );
foreach ( $author_terms as $term ) {
echo $term->slug; // the slug is the author ID
}
Further advantages:
- These are the same APIs used for categories and tags, they’ll be significantly faster than using user meta
- No race conditions on term counts
- Term counts are cached sometimes
- You’ll have a taxonomy, which means archive templates for each term and a URL structure for free!
You’ll want your taxonomy to be non-hierarchical, you’ll probably want to change the prefix on the name I gave it to something more unique to you ( don’t just call it followers ), and if you want a user interface in the admin interface you’ll want to read this
The only other note is you may want to delete the term associated with a user when they’re deleted. To delete the term or remove all of a users followers:
wp_delete_term( $author_id, 'wpse_180398_followers' );
To reset who a user follows to nobody:
wp_delete_object_term_relationships( $user_id, 'wpse_180398_followers' );
Tips for The Button Itself
As you already have an AJAX endpoint in your question, you can remove most of the code inside it and replace it with the snippets above. As for the button, you’ll want an element:
<a class="follow-button">Follow</a>
That’s also a toggle, so it’ll need something to indicate if you’re already following or not:
<a class="follow-button following">Follow</a>
Of course if it’s got the following
class, change how it looks like when you see a twitter follow button. You’ll only want the following class if you’re actually following the person though:
<?php
$following = '';
if ( has_term.. etc as above ) {
$following = ' following';
}
?>
<a class="follow-button<?php echo $following; ?>">Follow</a>
And of course it needs the ID of the author somehow, lets use a data attribute:
<?php
$following = '';
if ( has_term.. etc as above ) {
$following = ' following';
}
?>
<a class="follow-button<?php echo $following; ?>" data-author="<?php echo $author_id; ?>">Follow</a>
I trust you know how to get the authors ID based on your question.
Finally, you’ll want some javascript so that when a user clicks on a follow button ( jQuery( 'a.follow-button').click
, it:
- checks if the element has the
following
class - If it does, it fires an AJAX to unfollow the author
- If it doesn’t, it fires an AJAX to follow the author
- It passes the author data value of the element (
.data( 'author' )
in jQuery ) - It toggles the
following
class on the element so that the user gets some feedback (.toggle('following')
) - Bonus points for adding an
follow-in-progress
class that adds a spinner in css and makes the javascript bail out if it’s picked up so you don’t duplicate the requests