Skip Navigation

[Resolved] Count number of custom fields in a cpt with empty values

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 9 replies, has 3 voices.

Last updated by Timothy 1 year, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2544067

I'm tying to count the number of custom fields with empty values in a cpt. So for example the number of cpts that have the hours field not set, the number of cpts with the email field empty, the number of cpt that have the phone field empty.

I tried to use conditionals and a [calculate] shortcode with 1+ inside, but putting that shortcode around the <wpv-loop>
seems to break the shortcode. I know I could make individual Views for each of these instances and use [wpv-found-count], but this requires creating over 10 Views for my use case.

I came across this post that creates a shortcode counter for an individual field, but I guess this won't work for multiple fields right?

https://toolset.com/forums/topic/how-to-count-no-of-custom-posts-containing-critical-in-field-importance/

Is there any easy way to do this?

Thanks

#2544261

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tim

Can you clarify where you want to display what?

You want to display separate counts for how many posts of some type are missing the hours field, how many posts of the same type are missing the email field, and similarly those missing the phone field, i.e. 3 separate totals (possibly more that you are not describing).

Should these be displayed in the context of a View? e.g. you will displaying some posts with the View, at the end of which it should output these separate totals?

I'm guessing not, that you just want to display these totals some where, and are talking about a View, because that's how you query posts with Toolset.

If that were the case I would create a custom shortcode where you could pass a field slug as a shortcode attribute, and you could then query for posts using get_posts where you check whether there is a value for the field or not.

You can these use the same shortcode in multiple places, just passing the field slug to be used in the query each time.

Please clarify if that's the kind of thing you intend, and if you need help with it.

#2544269

Sorry, I left out some key details. Yes, I want to display separate counts for how many posts of some type are missing certain fields (hours, email, phone, etc.). And yes it should output these separate totals, like:

Phone missing: 10
Emails missing: 5
Hours missing: 3

Ideally I could also list the posts that are missing each.

"If that were the case I would create a custom shortcode where you could pass a field slug as a shortcode attribute, and you could then query for posts using get_posts where you check whether there is a value for the field or not."

That sounds like the solution, can you assist with that? It seems like the post I referenced above has code that does exactly that, except just for one field.

#2544701

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To display such summery - it is not required to add the shortcode within your view's loop.

Considering you created the following custom fields with slugs using Toolset (phone, emails, hours) and all those fields (phone, emails, hours) are assigned to post type "book":

You can add the following custom shortcode to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_shortcode( 'show_empty_count', 'func_get_empty_field_count');
function func_get_empty_field_count($atts) {
    $attributes = shortcode_atts( array(
        'field' => '',
    ), $atts );
    extract( $attributes);

  $post_type = 'book';
  $field = "wpcf-".$field;

    $args = array(
        'post_type' => $post_type,
       'meta_query'=> array(
            'relation' => 'OR',
            array(
               'key' => "$field",
               'value' => '',
               'compare' => '=',
        ), array(
               'key' => "$field",
              'compare' => 'NOT EXISTS',
          )),
        'posts_per_page' => -1
    );
    $query = new WP_Query($args);

    return  count($query->found_posts);
}

Now, where ever you want to display the total missing count, you can call the above shortcode as given under:

Phone missing: [show_empty_count  field="phone"]
Emails missing: [show_empty_count  field="emails"]
Hours missing:  [show_empty_count  field="hours"]

I hope solution shared above will help you to resolve your issue.

#2544719

Thanks. I've put this code in my functions file and replaced "book" with my post type name "place" and added the shortcodes to a Page, but they all indicate "1" which is not accurate. I also see this error:

Warning: count(): Parameter must be an array or an object that implements Countable

#2544723

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL where you added the shortcode I shared as well as admin access details and what is your expected count for all entities (phone, email, hours)?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site.

I have set the next reply to private which means only you and I have access to it.

#2544757

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've adjusted the code as given under and moved the code from functions.php file to "Custom Code" section offered by Toolset:
=> hidden link

// shortcode to count number of empty fields on Progress Page
add_shortcode( 'show_empty_count', 'func_get_empty_field_count');
function func_get_empty_field_count($atts) {
    $attributes = shortcode_atts( array(
        'field' => '',
    ), $atts );
    extract( $attributes);
 
  $post_type = 'place';
  $field = "wpcf-".$field;
 
    $args = array(
        'post_type' => $post_type,
       'meta_query'=> array(
            'relation' => 'OR',
            array(
               'key' => "$field",
               'value' => '',
               'compare' => '=',
        ), array(
               'key' => "$field",
              'compare' => 'NOT EXISTS',
          )),
        'posts_per_page' => -1
    );
    $query = new WP_Query($args);
  
 
    return $query->post_count;
}

Can you please confirm it works as expected now.

#2544759

Excellent! Working as expected now. But why did it have to be moved to Toolset Custom Code section? Was that part of the issue? Seems like that shouldn't have had any effect right?

#2544763

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

But why did it have to be moved to Toolset Custom Code section? Was that part of the issue? Seems like that shouldn't have had any effect right?
==>
When we add such custom code to "Custom code" section it will allow us to make edits and debug without any hassle and if we do the same with functions.php file then if there is error the whole site will be down and we will require FTP access to make edits to functions.php file.

The only change with the code is the following line from:

return  count($query->found_posts);

To:

  return $query->post_count;

We always recommend to use "custom code" section as it will offer you ease and all custom code will be found at the same place. In addition to that if you switch the theme - the custom code will be still there - you will not have to move it to another theme's functions.php file when you switch the theme so there are many advantages of using "custom code" section offered by Toolset.

I hope all things are clarified and you are welcome to mark resolve this ticket.

#2544775

My issue is resolved now. Thank you!