Skip Navigation

[Resolved] Search for a value in a custom field in all existing post

This support ticket is created 4 years, 4 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
- 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 6 replies, has 2 voices.

Last updated by WeiS2074 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1690847

Hi,

I have a custom type typeA, a custom field fieldA belongs to typeA. Can I make the following approach?
Here is the scenario, a user create a typeA via post form, after he enter the value in fieldA, it will search fieldA in all existing typeA post. If it found the same value as user entered, show an error message and not allow it to submit the form.

#1690969

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Yes, its perfectly possible to validate your custom field and you can check your field input value against existing field values.

You can use the Toolset form's hook: cred_form_validate
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

For example:
- Please try to add the following code to your current theme's functions.php file OR
"Custom Code" Section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter('cred_form_validate','func_validate_custom_field',10,2);
function func_validate_custom_field($error_fields, $form_data){
  
    //field data are field values and errors
  
    list($fields,$errors)=$error_fields;
  
     if ($form_data['id']==9999){
          
        $args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-EMAIL-FIELD-SLUG',
                              'value' => $_POST['wpcf-EMAIL-FIELD-SLUG']
                            )),
                    'post_type' => 'your-post-type',
                    'posts_per_page' => -1
                    );
            $posts = get_posts($args);
  
  
        //check if birthday value is already on the database
  
        if (count($posts) > 0){
  
            //set error message for my_field
  
            $errors['wpcf-EMAIL-FIELD-SLUG']='This E-mail already registered.';
  
        }
  
    }
  
    return array($fields,$errors);
  
}

Where:
- Replace 9999 with your original Form ID
- Replace EMAIL-FIELD-SLUG with original field slug
- Replace your-post-type with your original post type
- Adjust the error message as required

#1692999
Capture.JPG

Thanks for the reply, it works.

Can I have a further question?
1. in line 15, how can I include 2 post types in the function?
2. when it shows error can it display a clickable URL link, so that user can click the link?
3. if the above point is not approachable, Can it set a hidden custom field URL to viewable, and set the value to this custom field?

#1693017

Minesh
Supporter

Languages: English (English )

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

1. in line 15, how can I include 2 post types in the function?
==>
You can add the post type slug as array.

'post_type' => array('posttype-slug1' , 'posttype-slug2'),

2. when it shows error can it display a clickable URL link, so that user can click the link?
==>
What kind of link you want to display?

3. if the above point is not approachable, Can it set a hidden custom field URL to viewable, and set the value to this custom field?
==>
I would like to know the flow - how exactly you want to add URL and display it?

#1696883

Here is my code. I want to set $ori_url to be clickable.

if (count($posts) > 0){
$ori_posts_id = $posts[0]->ID;
$errors['wpcf-cf-url']=strval($ori_posts_id);
$ori_url = get_permalink( $ori_posts_id );
//set error message for my_field
$errors['wpcf-cf-url']='This product has been posted, please go to ' . $ori_url ;
}

#1697659

Minesh
Supporter

Languages: English (English )

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

What if you try to use the following code:

if (count($posts) > 0){
$ori_posts_id = $posts[0]->ID;
$errors['wpcf-cf-url']=strval($ori_posts_id);
$ori_url = get_permalink( $ori_posts_id );

$click_url = '<a href="'.$ori_url .'"> Click Here </a>';
//set error message for my_field
$errors['wpcf-cf-url']= 'This product has been posted, please go to ' .$click_url ;
}

Where:
Change the "Click Here" with your desired caption text.

#1697661

This is exactly what I want. Appreciated.