I have a table that is listing the Property cpts and in columns I am displaying:
1. custom number field for the cost
2. custom checkbox field for photo quality
3. a child view that counts the number of reviews the parent Property cpt has
hidden link
I'd like to count the number of "empties" for each of these columns. So the number of properties that do not have a cost set, the number of properties that do not have the photo quality checkbox checked, and the number of properties that have no reviews.
For 2 I used the solution here to count the photo quality checkbox and that works great.
https://toolset.com/forums/topic/help-with-item-count/
II'm not sure how to go about 1 and 3, counting the number of posts that have their cost field empty and counting the number of posts that have 0 child review posts.
Thanks,
Tim
Hi Tim,
Thank you for contacting us and I'd be happy to assist.
While you can use the custom shortcode approach suggested in the other ticket to get the count of the posts with those missing field values, it can result in performance impact.
(as we'll be using 3 separate queries for each of these 3 items, on top of the queries of the involved views)
One way to avoid running additional queries is by using a custom script, that cycles through each row in the table and counts the number of specific columns where each of those items is missing.
For example, in the view's "JS editor" you can include the following script:
jQuery(document).ready(function( $ ) {
// missing Cost Count
var missCostCount = 0;
$('.js-wpv-view-layout table#example tbody > tr > td:nth-of-type(3) ').each(function () {
var $this = $(this);
var selText = $this.text();
if (selText.indexOf("Empty") >= 0) {
missCostCount++;
}
});
$('.js-wpv-view-layout span#missing-cost-count').text(missCostCount);
// missing Photos Count
var missPhotosCount = 0;
$('.js-wpv-view-layout table#example tbody > tr > td:nth-of-type(4) ').each(function () {
var $this = $(this);
var selText = $this.text();
if (selText.indexOf("Needs better photos") >= 0) {
missPhotosCount++;
}
});
$('.js-wpv-view-layout span#missing-photos-count').text(missPhotosCount);
// missing Reviews Count
var missReviewsCount = 0;
$('.js-wpv-view-layout table#example tbody > tr > td:nth-of-type(5) ').each(function () {
var $this = $(this);
var selText = $this.text();
if (selText.indexOf("No Reviews") >= 0) {
missReviewsCount++;
}
});
$('.js-wpv-view-layout span#missing-reviews-count').text(missReviewsCount);
});
And in the view, where you'd like to show the count of each of those missing items, you can include span tags with special IDs, like this:
Properties missing costs: <span id="missing-cost-count"></span>
Properties missing reviews: <span id="missing-reviews-count"></span>
Properties needing better photos: <span id="missing-photos-count"></span>
The script will find these span tags and will populate the count of those missing items, within these span tags.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Thanks for this. This will be a good solution I think, but I can't seem to get it to work. I believe I have to replace the "$" with "jQuery" in the code right? And do the cells have to only have that word? For example "Empty" without any other words or icons?
Tim
Actually I got this working, I'm all set now, thanks!
Thanks for the update and glad that it is working now.
You're welcome to mark this ticket as resolved and start a new one, for each new question or concern.
My issue is resolved now. Thank you!