Skip Navigation

[Resolved] Counting the number of posts with an empty field value and with no child posts

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Timothy 1 year, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2522865

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

#2523753

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

#2524225

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

#2524283

Actually I got this working, I'm all set now, thanks!

#2524753

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.

#2524933

My issue is resolved now. Thank you!