Skip Navigation

[Resolved] Parametric search for repeating fields: number of fields between x and y

This support ticket is created 7 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by JackO 7 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#481061

Hello,

I use a repeating field in a CPT. What I would like to be able to do, is to use a parametric search to search for the number of repeating fields. Something like:

list the posts with a number of repeating fields between x and y
list the posts with a number of repeating fields of a maximum of x, etcetera

I know how to set up a search like this for non-repeating fields, but how do I do this with repeating fields?

Thank you!

#481149

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jack,

Thank you for contacting our support forum.

If I understand correctly you want something like this -> Display all posts where #of image fields is greater than 4

So you will get a list of all the posts that have more than 4 repeated image fields correct?

If so then no this wouldn't be possible because our filter queries the value in the field and not the number of fields itself.

Thanks,
Shane

#481165

Hi Shane,

Yes, that is what I would like to do. Not for images, but for other content, but it is the same principle. Could a solution be the following?

- count the number of fields when saving the CRED-form and write the result as a number to a separate field, using the cred_save_data hook?
- then I could use this number in the separate field for the search filters

If this would be the right direction, how could I do it? I found the following snippet on the forums to count the number of repeated fields as a shortcode, but I struggle to rewrite it to use it for the cred_save_data hook.

// shortcode to count number of repeating field instances
add_shortcode( 'count-repeat', 'count_repeat_func' );
function count_repeat_func($atts) {
   return sizeof(get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false ));

Thank you!

#481398

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jack,

Yes this should work if the correct count is returned.

What appears when you place that shortcode on one of your post content ?

Does it return the actual count of the field ?

If it does then we can modify this to work with a cred Hook but we need to verify that the correct count is being returned.

Looking forward to hearing from you soon.

Thanks,
Shane

#481426

Hi Shane,

Yes, the shortcode returns the correct count.

However, I did have to modify the code a bit, following these instructions:
https://toolset.com/forums/topic/how-to-count-repeated-field/#post-412707

The working code for the shortcode is:

// shortcode to count number of repeating field instances
add_shortcode( 'count-total', 'count_repeat_func' );
function count_repeat_func($atts) {
    return sizeof(get_post_meta( $atts['post-id'], 'wpcf-' . $atts['field'], false ));
}
#481487

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jack,

Thanks for confirming.

Now for the hook to create the count for CRED.

So what you need to do is add the following to your functions.php file and it should create a new custom field with the count inside it.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
 $count = sizeof(get_post_meta( $post_id, 'wpcf-field', false ));
    
            // add it to saved post meta
            add_post_meta($post_id, 'my_count', $count, true);
        
    }
}

Now what you need to do is just replace the wpcf-field with the name of the Types field you want to check and change the 12 to the id of you form.

Please try this and let me know if it helps.

Thanks,
Shane

#481565

Hi Shane.

Thank you for this code. I implemented this on my site and it works great! 🙂

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.