Skip Navigation

[Resolved] Front End Post Form – Auto Relationship with Parent

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

Last updated by JonathanK9428 6 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1052214

We have a custom post type named supplier, where a user can sign up and they're only allowed to submit 1 supplier (business). We also have another custom post type named supplier-product, where the user should have the ability to submit multiple products associated with their supplier. We did configure the relationships being one-to-many where:
Suppliers [0 .. 1] << Supplier Products [*]

In the post form for supplier-product, there was an option to add a relationship field, but it gives the user the ability search through all the suppliers posted on the website, even suppliers posted from other users, and choose any supplier. Is there anyway we can limit this list to only their posted supplier, or even better, auto-relate the supplier-product with the supplier posted by the logged in user?

Here's the relationship field that was inserted into the form:

<div class="form-group">
<label>Parent Business</label>
[cred_field field='@supplier-product.parent' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>

Thanks

#1069187

Hello,

I assume you are going to relate the specific "supplier" post whose author is current user.

It is possible to do the auto-relate with some custom PHP codes, for example
1) after user submit the form for creating "Supplier Products" posts, you can use Toolset form action hook cred_save_data to trigger a custom PHP funciton
2) In this PHP function,
get the post ID of "supplier" post,
https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
And relate the new "Supplier Products" post with "supplier" post with API function toolset_connect_posts()
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

#1069341

Thank you for your feedback. Excuse my coding, but would something along the lines of this work in my custom functions php file?

// Connect the parent Supplier to submitted Products
function connect_supplier_products() {
    
    // Variable for storing the supplier id associated with the logged in user
    $get_supplier_id = get_posts( array(
     
    // We define the Post type 
    'post_type' => 'supplier',

    // Retrieve the current ID of the logged in user
    'author'    => get_current_user_id(),
) );
    
    // Get the ID of the new Product Post
    $postid = get_the_ID();
    
    // Connect supplierid with the new productid
	toolset_connect_posts( 'supplier-product', $postid, $get_supplier_id );
}
#1069960

Since it is a custom PHP codes problem, please provide a test site with the same problem, and fill below private message box with credentials and FTP access, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug.

#1069979

I have tried the credentials you provided above, but get this error:

This site can't be reached
semiconlink.com took too long to respond.

Please check it.
And my IP address is: 119.28.140.171

#1069981

Should be all set, just whitelisted your IP.

#1069988

Thanks for the details, I have modified your PHP codes as below:

// Connect the parent Supplier to submitted Products
add_action('cred_save_data', 'connect_supplier_products',10,2);
function connect_supplier_products($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']!=301) {
        return;
    }
        
    // Variable for storing the supplier id associated with the logged in user
    $get_supplier_ids = get_posts( array(
        // We define the Post type 
        'post_type' => 'supplier',
        // Retrieve the current ID of the logged in user
        'author'    => get_current_user_id(),
        'fields'	=> 'ids',
	) );
    
    if(isset($get_supplier_ids[0])){
        $supplier_id = $get_supplier_ids[0];
    }
    
    if($supplier_id){
    	// Connect supplierid with the new productid
		toolset_connect_posts( 'supplier-product', $supplier_id, $post_id );
    }
}

Please test again, check if it is fixed, you can also remove the the relationship field from post form:
hidden link

[cred_field field='@supplier-product.parent' select_text='--- not set ---' class='form-control' output='bootstrap']
#1070468

Thank you so much, that did the trick!

Would make a great feature for post forms on the front-end to have the capability to relate data if relationships are configured, that way you don't need to write any custom functions. May be a great benefit for Toolset and it's users.