php - Return A List of Authors of a Post Type Connected to Another Post Type, Plus A Count of Those Posts By Each Author

Custom Post Types:-
- Cards : 'cards'
- Card Sales : 'card_sales'
Related Plugins:-
- Gamipress
- Advanced Custom Fields
Summary: I have a Custom Post Type called 'cards' which is actually an Achievement Type of the Wordpress plugin, Gamipress. On each Card single template, I have an ACF frontend form which allows users to 'sell' the Cards and creates a Card Sales post at the same time. There are hidden fields on the form: one is a number field which is prefilled with the current User ID and the other is an ACF Relationship field called 'card_sold' that is prefilled/preselected with the Card post that is currently being viewed. A custom function then runs on submission of the form for the selling of the Card which I don't need to go into here, but the key thing is each Card Sales post has an Author (who was the current user who created post) and a connected Card post to show which Card was sold.
So on each Card single template I want to show which Users have sold this Card and how many they have sold. This would be achieved by getting the Card Sales posts that have the currently viewed Card included in its ACF Relationship field and counting how many of those are by each Author.
Please see the following images:
You'll see that I'm already showing a count for how many times the current user has sold this Card and how many have been sold in total. Below that I then have a count for how many Users sold the Card. It says 3 because it's counting the Card Sales posts this Card is included in, but actually only 2 users have sold the Card - myself (Chris) 2 times and a user called FireglassStudios 1 time. In the second image you can see the modal that pops up when clicking the '3 Fans' count button. I know that the issue here is that I'm getting the Card Sales posts and echoing the user info for each of the posts but how can I group them so that the count button says '2 Fans Sold This Card' and the list in the modal says:
Chris (2 Sales) FireglassStudios (1 Sale)
Here is the full code I currently have:
<?php
$soldCardsTotal = get_posts(array(
'post_type' => array( 'card_sales'),
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'card_sold',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
)); ?>
<div class="user-card-sold-count">
You Have Sold <?php echo count( $soldCards ) ; ?> of This Card
</div>
<div class="total-card-sold-count">
Total Sales of This Card On Site: <?php $soldCardsTotalCount = count( $soldCardsTotal ) ; echo $soldCardsTotalCount; ?>
</div>
<span class="collection-count">
<button id="sellers">
<?php if ($soldCardsTotalCount > 1) {
echo "$soldCardsTotalCount Fans";
} else if ($soldCardsTotalCount == 1) {
echo "$soldCardsTotalCount Fan";
} else {
echo "$soldCardsTotalCount Fans";
} ?>
</button> Sold This Card
</span>
<div id="SellersModal" class="modalSellers modal">
<div class="modal-window-sellers modal-window">
<span class="close-sellers">Г—</span>
<div class="fan-heading"><i class="fas fa-user" style="margin-right:8px;"></i>Fans Who Sold This</div>
<div class='fan-block'>
<?php if($soldCardsTotal): ?>
<?php foreach($soldCardsTotal as $s): ?>
<?php $sellerID = $s->post_author; ?>
<div class="fan-item">
<div class="fan-image">
<a href="<?php echo get_author_posts_url($sellerID); ?>"><?php echo get_avatar($sellerID); ?></a>
</div>
<div class="fan-title">
<a href="<?php echo get_author_posts_url($sellerID); ?>">
<?php the_author_meta( 'display_name' , $sellerID ); ?></a> (<?php echo count($s); ?> Sales)
</div>
</div>
<?php endforeach; ?>
<?php else : ?>
<div class="empty-message">
No One Has Sold This Card Yet
</div>
<?php endif; ?>
</div>
</div>
</div>
<script>
var modalSellers = document.getElementById("SellersModal");
var btn = document.getElementById("sellers");
var span = document.getElementsByClassName("close-sellers")[0];
btn.onclick = function() {
modalSellers.style.display = "block";
}
span.onclick = function() {
modalSellers.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modalSellers) {
modalSellers.style.display= "none";
}
}
jQuery(".modal").click(function(e){
let $this = jQuery(this);
if (jQuery(e.target).hasClass("modal")) {
$this.css('display', 'none');
}
})
</script>
What I've Tried:
- I've tried a foreach on the $sellerID because I assume I need the users, not the posts but it returns empty results.
- I've tried a WP_User_Query with 'author__in' => '$sellerID but it lists all users of the site.
Hopefully I've been as clear as possible. If any more details are required, please let me know. Thanks for any assistance anyone can provide.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: zsh: command not found: php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.