Skip Navigation

[Resolved] Make sure a value is unique in the database based on parent relationship

This thread is resolved. Here is a description of the problem and solution.

Problem:

Query child posts of one-to-many relationship using PHP codes.

Solution:

You can follow our document to setup the custom codes:

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 3 years, 9 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by ezraK 3 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1706007

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?

#1706385

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

#1707315

The relationship is created with Types. I'm just not understanding the syntax required in place of '__KEY__' and '__VALUE__'.

#1707537

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.

#1711577

My issue is resolved now. Thank you!

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