Tell us what you are trying to do?
I am trying to check if a "username" already exists for a selected client->account relation. I have not been able to figure out what I need for '__KEY__' and '__VALUE__' to check through accounts that are related to the selected parent only.
[code]
add_filter('cred_form_validate','func_validate_wc_username',10,2);
function func_validate_wc_username($error_fields, $form_data){
//field data are field values and errors
list($fields,$errors)=$error_fields;
if ($form_data['id']==41 || $form_date['id']==42){
$args = array(
'meta_query' => array(
'relation' => 'AND',
array('key' => 'wpcf-wireless-account-username',
'value' => $_POST['wpcf-wireless-account-username'],
'compare' => '='
),
array('key' => '__KEY__',
'value' => '__VALUE__',
'compare' => '='
)),
'post_type' => 'wireless-account',
'posts_per_page' => -1
);
$posts = get_posts($args);
//check if username value is already in the database.
if (count($posts) > 0){
//set error message for my_field
$errors['wpcf-wireless-account-username']='Please select a different username.';
}
}
return array($fields,$errors);
}
[/code]
What is the link to your site?
Hello,
It is a custom codes problem, please elaborate the questions with more details:
I have not been able to figure out what I need for '__KEY__' and '__VALUE__' to check through accounts that are related to the selected parent only.
Provide detail steps to duplicate the same problem:
How do you setup the "__KEY__" field and '__VALUE__' value?
How do you setup the client->account relationship? Is it a post type relationship created with Types plugin?
If it is, I suggest you try to follow our document to setup the custom codes:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Or you can try with standard WP class WP_Query, see the examples here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#new-approach
The relationship is created with Types. I'm just not understanding the syntax required in place of '__KEY__' and '__VALUE__'.
It is still not clear, please provide detail steps to duplicate the same problem:
1) How do you setup the post type relationship client->account, is it one-to-one or one-to-many relationship?
2) how do you setup the custom "__KEY__" field? in "client" or "account" post type?
The custom PHP codes you mentioned above, it seems to be querying "account" posts, you are going to query related "account" posts by current "client" post, isn't it?
If it is, please check the documents I mentioned above, for example, you can setup relationship in your PHP codes like this:
...
'toolset_relationships' => array(
'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => 'client-account' // client-account relationship slug
),
...
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#new-approach
If you need more assistance for it, please provide a test with the same problem, also point out the problem page URL and where I can edit your PHP codes.
My issue is resolved now. Thank you!