[Résolu] Parametric search for repeating fields: number of fields between x and y
This support ticket is created Il y a 7 années et 3 mois. 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.
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.
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?
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 ));
// 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 ));
}
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.