Skip Navigation

[Resolved] Adjust content of relationship field in cred form

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/Karachi (GMT+05:00)

Author
Posts
#2378967
Screenshot 2.png
Screenshot 1.png

I am creating a biography and stories website with Toolset and Oxygen, for a client.

Stories and biographies are written by users that can register to be able to add content.
Registered user can choose to write a simple article about a person or a company, or write a full biography (cpt BIOGRAPHY related to cpt CHAPTERS by "one to many" relationship).
Users add all content via CRED FORM because my client wants to prevent access to the backend.

I created PEOPLE and COMPANIES cpts and created a “one to many” relationship with ARTICLES.
I then created BIOGRAPHY and COMPANY BIO cpts and set a “one to one” relationship with PEOPLE and COMPANIES respectively, so a person or a company can only have one biography related to them.

I then created all necessary forms to allow users to add content from the frontend, adding the relationship field who would allow users to connect their content either to a person or a company. I have first developed the part related to People and their biographies and bumped into the following problem.

While testing the website and adding content from the front-end via cred forms , I found out that while in the “one to many” relationship case everything seems to work fine, I cannot say the same thing with the “one to one” relationship.
In fact:

1. On cred form on the frontend, the relationship field outputs a list of people INCLUDING those that already have been assigned a biography, while this doesn’t happen if I add a biography from the back-end.
2. If the user selects a person that has already been assigned a biography, the form gets submitted without errors but inside of it the relationship with the person has not been registered, therefore missing a crucial piece of information for the admin that has to approve and publish content.

I hope I explained myself. Is there a way to restrict the list of people (and companies) available in the relationship field in cred form, if they already have been assigned a biography?
I can of course provide full access to the website if needed.

#2379437

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

From your message, I understand that you're using the "add new biography" form to also connect the related person/company posts.

To make sure that only non-connected person/company posts can be connected, there are two options:

1. A simpler workaround is to not connect the related person/company posts through the form that creates a new biography. Instead, you can use a separate relationship form at the next step, which will not allow connecting post that already has a connection.

OR

2. If you'd prefer to use the same form that creates a new biography for this, you'll need to add some customizations:

a). You'll remove the relationship field to select the related person/company and instead add a select type generic field. The options in this field can be programmatically created from the available person/company posts, excluding the ones, which are already connected.

Here is an example of a custom shortcode to generate this field's options:


add_shortcode('add_people_field', 'add_people_field_func');
function add_people_field_func() {

	// get all the people posts
	$args = array(
		'post_type'        => 'people_post_type_slug',
		'posts_per_page'   => -1,
		'post_status'      => 'publish',
	);
	$posts_array = get_posts( $args );

	foreach ($posts_array as $post) {
		// check if a related post already exists
		$related_id = toolset_get_related_post( $post->ID, 'people-biography_relationship_slug' );
		// if it doesn't include in the options
		if(!$related_id) {
			$data[] = array('value' => $post->ID, 'label' => $post->post_title ); 
		}
	}

	// return the them in json format for the generic field
	return(json_encode($data));

}

Note: You'll replace "people_post_type_slug" with the actual slug of your person/company post type and "people-biography_relationship_slug" with the slug of the relationship between them.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

b). In the form to add a new biography the generic field code will look like this:


[cred_generic_field type='select' field='people-field']
{
"options":[add_people_field]
}
[/cred_generic_field]

As a result, you'll have a select field in the form that will only show the person/company posts which are not already connected.

c). The last step would be to process this selected person/company post's ID and connect it with the newly created biography post when the form is submitted:


add_action('cred_save_data', 'custom_people_save_data_action',10,2);
function custom_people_save_data_action($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==1234)
	{
		$post_from_gen_field = $_POST['people-field'];
		if(!empty($post_from_gen_field)) {
			toolset_connect_posts( 'people-biography_relationship_slug', $post_from_gen_field, $post_id );
		}   
	}
}

Note: You'll replace 1234, with the actual ID of your post form and "people-biography_relationship_slug" with the slug of the relationship. You can learn more about the "cred_save_data" hook and the "toolset_connect_posts" function, from these links:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

The above code snippet too can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2379461

Hello Waqar thank you for helping me solve this.

I just implemented it with the "people" part and for some reason it is not working. I still can see in the list the two people that have already a biography assigned to them.
Do you need access to the website to see if I am doing something wrong?

Thank you so much for your help!

#2379503

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back and I'd be happy to take a look.

Can you please share temporary admin login details, along with the link to the page where this form can be seen?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2379555

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing the admin access.

The parent/child post type direction in my website's relationship was different than your setup, which is why the shortcode was not excluding the already connected persons.

I've updated the custom shortcode slightly and the exclusion is working now:


add_shortcode('add_people_field', 'add_people_field_func');
function add_people_field_func() {
 
    // get all the people posts
    $args = array(
        'post_type'        => 'persona',
        'posts_per_page'   => -1,
        'post_status'      => 'publish',
    );
    $posts_array = get_posts( $args );
 
    foreach ($posts_array as $post) {
        // check if a related post already exists
        $related_id = toolset_get_related_post( $post->ID, 'biografia-di-persona', 'child' );
        // if it doesn't include in the options
        if(empty($related_id)) {
            $data[] = array('value' => $post->ID, 'label' => $post->post_title ); 
        }
    }
 
    // return the them in json format for the generic field
    return(json_encode($data));
 
}

I also submitted a test biography entry (test from TS support) and it correctly connected with the selected person post, which confirms that the second code snippet for the "cred_save_data" hook is also working as expected.

#2379577

Waqar I really don't know how to thank you!
Now all I have to do is duplicate this for companies.
One more question: what if I wanted the list of people or companies in the form's generic field to be sorted by Post Title?

Thanks again!!!

#2379617

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You're welcome and glad I could help.

To adjust the order of the posts in the custom shortcode, you can include the 'orderby' and 'order' parameters, in the "$args" array:


$args = array(
	'post_type'      => 'persona',
	'posts_per_page' => -1,
	'post_status'    => 'publish',
	'orderby'        => 'title',
	'order'          => 'ASC',
);

The "get_posts" function used in the custom shortcode supports the arguments from the "WP_Query" class and here are usefully links on the topic:
https://developer.wordpress.org/reference/functions/get_posts/
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

#2379627

Thank you Waqar, I had a chance to learn a great deal about Toolset thanks to your detailed explainations. Thank you so much for your precious help!!!! 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.