Skip Navigation

[Resolved] List author's content only (in the selection field)

This support ticket is created 5 years, 3 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.

Our next available supporter will start replying to tickets in about 0.28 hours from now. Thank you for your understanding.

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)

This topic contains 14 replies, has 2 voices.

Last updated by Waqar 5 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1196282

Tell us what you are trying to do?

Hello,

I've made one-to-one relationship. There are TEAMS and PROJECTS. Each Team can be assigned to One Project only.

1. Here I have the first post Form where I can submit some Team(s): hidden link

2. Then here I can see (as the author) my team successfully listed here: hidden link

2. Then I continue with submitting some Project by another post Form:
hidden link

There is a relation with the previous post Form – here I should assign some Team as the author of this Project (in this example I'm assigning previously submitted the Team called "Team JIRIK")

My problem: As you can see - in the list of Teams I should see my Team (JIRIK) listed here only BUT I can see all other Teams registered by other authors.

Here is the code from the "Projects" Post Form (I don't know if the problem is here):

<div class="form-group">
<label>Tým studentů</label>
[cred_field field='@tym-studentu.child' select_text='--- Vyberte tým ---' class='form-control' output='bootstrap']
</div>

Many thanks for your help and a suggestion!

Best,

Jiri.

#1196486

Waqar
Supporter

Languages: English (English )

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

Hi Jiri,

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

The "cred_field" shortcode accepts "author" attribute, which can be used to restrict posts from a specific author.
( ref: https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field )

For example, to restrict the list to only current logged-in user's posts, you can include author='$current':


[cred_field field='@tym-studentu.child' select_text='--- Vyberte tým ---' class='form-control' output='bootstrap' author='$current']

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

regards,
Waqar

#1196637

Hello Waqar,

many thanks for your help and the suggestion - it solved this problem and it works fine now 🙂 .

One more question about this selection field:

I need to translate "Searching" and "No results found" to my language, please see:

hidden link
and
hidden link

I'm using WPML which is also your product and I wasn't able to translate it.

Thanks again, best,

Jiri.

#1196721

Waqar
Supporter

Languages: English (English )

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

Hi Jiri,

Thanks for the update and glad it worked.

The strings/messages that you referred to are currently not translatable, I'm afraid, but this limitation has already been reported to the concerned team.

I don't have a time estimate to share at this time, but I will update you through this ticket, as soon as these text strings are made, translation-friendly.

You're welcome to open a new ticket for a new question or concern.

regards,
Waqar

#1196857

Dear Waqar,

thanks for your explanation - I didn't expect that it is impossible to translate these strings - that's why I decided to use WPML on this site to make all these issues translatable...

I still have several problems with this Post Form:

1. Here I'm trying to fill in the form. As you can see there are no thumbnails of the uploaded photos hidden link

and

2. ...after the submission of the Post Form I can't see the photos and there is also the warning about missing PDF file. hidden link

3. I've made one-to-one Relationship. Each Team should be possible to assign to ONE Project ONLY.

a) The first Team was created and correctly connected to the Team hidden link

b)Then I was trying to assign the same Team to some other project - it means that I was trying to "break" relationship rules.

As the result it WAS POSSIBLE to choose and save the project WITH NO WARNING - the Project was created and it was not just not connected to the Team hidden link

So I need to display some warning saying that "It is impossible to connect one Team to more than ONE Project." to restrict saving the Post Form when One-to-One relationship doesn't allow it.

Thanks again for your help.

Jiri

#1196986

Dear Waqar,

I was able to resolve issues 1 and 2 - it was the hosting problem.

So I still need to find a solution for this:

3. I've made one-to-one Relationship. Each Team should be possible to assign to ONE Project ONLY.

a) The first Team was created and correctly connected to the Team hidden link

b)Then I was trying to assign the same Team to some other project - it means that I was trying to "break" relationship rules.

As the result it WAS POSSIBLE to choose and save the project WITH NO WARNING - the Project was created and it was not just not connected to the Team hidden link

So I need to display some warning saying that "It is impossible to connect one Team to more than ONE Project." to restrict saving the Post Form when One-to-One relationship doesn't allow it.

Thanks again for your help.

Jiri

#1197063

Waqar
Supporter

Languages: English (English )

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

Hi Jiri,

Thanks for the update.

Instead of adding a warning message, I'll recommend a workaround that will make it impossible to select a team that is already assigned to a different project.

I understand that you have a post form to add a new project, which connects the project to a team ( hidden link )

To make the team selection field, only show those teams from the current user, which are not linked to any project, you can register a custom shortcode, by adding the following code in the active theme's functions.php file:


// generate options for the team field
add_shortcode('get_teams_field', 'get_teams_field_fn');
function get_teams_field_fn() {

	// get all team posts from current user
	$args = array(
				'post_type'        => 'team',
				'posts_per_page'   => -1,
				'post_status'      => 'publish',
				'author'		   => get_current_user_id()
			);

	$posts_array = get_posts( $args );

	foreach ($posts_array as $post) {

		// check if relationship exists
		$query_by_element = $post->ID; // ID of post to get relationship from
		$relationship = 'team-project'; // relationship slug
		$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
		$limit = 100; // defaults
		$offset = 0; // defaults
		$args = array(); //nothing needed
		$return = 'post_id'; // We want Post ID
		$role_name_to_return = 'child'; // We want children.
  
		$get_results = toolset_get_related_posts(
						$query_by_element,
						$relationship,
						$query_by_role_name,
						$limit,
						$offset,
						$args,
						$return,
						$role_name_to_return
						);

		if (!$get_results) {
			$data[] = $arrayName = array('value' => $post->ID, 'label' => $post->post_title );
		}
		
	}

	return(json_encode($data));

}

Notes:

1. Please make sure that "team" is replaced with the actual slug of your team post type. Similarly, please also make sure that that "team-project" slug is replaced by the actual slug of your team-project relationship.

2. Also the above code assumes that team post type is the parent and the project post type is the child in the relationship. If they are setup conversly on your website, you can inverse the values for "$query_by_role_name" and "$role_name_to_return" too.

Once added, you can replace your shortcode for the team selection field in the form with a generic field shortcode, that uses this new shortcode:


[cred_generic_field type='select' field='@team-project.parent']
{
"required":1,
"default":[],
"options":[get_teams_field]
}
[/cred_generic_field]

Note: Please replace "@team-project.parent" with the actual relationship slug and role of the team post type.

Important note: For future reference, please note that as per our support policy ( https://toolset.com/toolset-support-policy/ ), only one question or issue can be addressed through a single ticket.

You're welcome to open a new ticket for each new question or concern.

regards,
Waqar

#1197170

Dear Waqar,

thanks again for your help!

This solution seems to be very good (and I will try to apply it for testing) but I'm still trying to look at this problem from the user's point of view. There is already a problem with untranslatable strings of the selection field and some users don't understand even these simple English information.

So imagine that a user is trying to select a Team and there are "Searching.." and "No results found" notification which could be quite confusing... And if he/she will try to assign some Team to more than one Project then there is no warning... it will just not allow doing that. And finally, a user would say... "It doesn't work and I don't know why".

Thanks again for your suggestion but I need to ask you for a solution with some warning saying that "It is impossible to connect one Team to more than one Project."

Thank you, best,

Jiri.

P. S. Do you want me to open a new ticket with this issue?

#1197320

Waqar
Supporter

Languages: English (English )

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

Hi Jiri,

Thanks for writing back and please do let me know, how my suggested approach turns out.

It is important to note here, that if you'll use a generic field to create a link between team and project (as suggested in my last message), the select field for the team dropdown would be a regular select field and not a "select2" type auto-fill drop-down field. As a result, the limitation of the untranslatable text would naturally be skipped.

Note: But it doesn't mean that issue won't be resolved or we won't update you once that happens. You are already in mine and Christian's follow-up list ( ref: https://toolset.com/forums/topic/how-to-translate-searching-and-no-results-found-in-forms-post-relationship-field/ ) and as soon as those text strings are translatable, you'll receive an update from us.

In terms of user experience too, you won't need to show a warning or information message, since the form would be only showing a list of teams, which are currently not assigned to any other projects.

Still, if you'd like this point to be more clear for your users, you can add some text on top or bottom of the form, explaining that this new project form will only link teams which are available and are not working on any other projects.

For a case where a user would need to connect a project to a team which is already working on a different project, you can create a new page with a new form that shows all teams. But do make this clear for users that this will remove the old relationship, as, in a one-to-one relationship, only one team can be linked to one project, at any given time.
( ref: https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/ )

regards,
Waqar

#1197337

Dear Waqar,
thanks again for your suggestion,

now I understand it and going to test a suggestion in your previous message. I will let you know if I'm able to make it.

Thank you for your patience.

Jiri

#1197559

Dear Waqar,

I was trying to follow your instructions but there is something wrong and it is not working as expected.

Here is my problem:

1. My Team slug is "tym" hidden link

2. My relationship slug is "tym-studentu" hidden link

As you can see: Projects are "parent" and Teams are "child"

So in my theme's functions.php I changed:

'post_type' => 'tym'
.
.
$relationship = 'tym-studentu'
$query_by_role_name = 'child'
.
.
$role_name_to_return = 'parent'

Here is the whole code:

// generate options for the team field
add_shortcode('get_teams_field', 'get_teams_field_fn');
function get_teams_field_fn() {

// get all team posts from current user
$args = array(
'post_type' => 'tym',
'posts_per_page' => -1,
'post_status' => 'publish',
'author' => get_current_user_id()
);

$posts_array = get_posts( $args );

foreach ($posts_array as $post) {

// check if relationship exists
$query_by_element = $post->ID; // ID of post to get relationship from
$relationship = 'tym-studentu'; // relationship slug
$query_by_role_name = 'child'; // $query_by_element is a parent in this relation
$limit = 100; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'parent'; // We want children.

$get_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);

if (!$get_results) {
$data[] = $arrayName = array('value' => $post->ID, 'label' => $post->post_title );
}

}

return(json_encode($data));

}

3. Here is the code what I put in in my Project's Post Form ( I have changed field='@team-project.parent' to field='@tym-studentu.child' :

<div class="form-group">
<label>Tým studentů</label>
[cred_generic_field type='select' field='@tym-studentu.child']
{
"required":1,
"default":[],
"options":[get_teams_field]
}
[/cred_generic_field]
</div>

4. After all of this, there is a warning on Project's Post Form hidden link and in general, it is not working as expected.

Please let me know what I did wrong and how to repair it.
Many thanks again.

Jiri

#1197654

Waqar
Supporter

Languages: English (English )

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

Hi Jiri,

All your steps seem to be correct and the warning message ( hidden link ) suggest that currently there are no "team" posts available on the website, which match the criteria.
(i.e. they belong to the currently logged-in user and are not already in a relationship with a "project" post)

To better handle this case and avoid this warning, you can update following block:


if (!$get_results) {
	$data[] = $arrayName = array('value' => $post->ID, 'label' => $post->post_title );
}

To:


if (!$get_results) {
	$data[] = $arrayName = array('value' => $post->ID, 'label' => $post->post_title );
}

if(empty($data))
{
	$data[] = array('value' => '', 'label' => 'No teams were found' );
}

Please add a new team on the website from the current user and make sure that it is not already connected to any project and then check the form again.

regards,
Waqar

#1197747

Dear Waqar,

OK, thank you again,
after changing the code...
finally, I am able to connect the teams and with the selection and now there are only not already connected teams available - which is great!

BUT

1. When I go to edit the Project which I made just a few seconds ago hidden link then at the bottom I can see that the Team is unconnected with the warning hidden link

Maybe there is a conflict with the previously added function for saving and closing the Project (but I'm not sure if this has some relation/reason for unconnecting already connected Team):

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==166)
{
// "Close and Send" button clicked
if (isset($_POST['form_submit_2']) && $_POST['form_submit_2'] == 'Uzavřít a odeslat přihlášku')
{
// saved post meta
update_post_meta($post_id, 'wpcf-closed', 1);
}
}
}

Here is the whole functions.php file: hidden link

2. I will need to translate "--- not set ---" string in the selection now. Previously I could put

select_text='--- Vyberte tým ---'

to

[cred_field field='@tym-studentu.child' select_text='--- Vyberte tým ---' class='form-control' output='bootstrap' author='$current' required='true']

but when now I can't put it here to get it working:

[cred_generic_field type='select' field='@tym-studentu.child' select_text='--- Vyberte tým ---']
{
"required":1,
"default":[],
"options":[get_teams_field]
}
[/cred_generic_field]

Thank you very much for your help and suggestion.

Jiri

#1198449

Waqar
Supporter

Languages: English (English )

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

Hi Jiri,

Thank you for waiting and sharing the update.

1. To troubleshoot, why the team is still showing disconnected, I'll need to see how the form and relationship are actually set up on the website.

If the issue still persists, you're welcome to share temporary admin access details, in reply to this message.

I've set your next reply as private so that you can share the login information, privately.

2. The "cred_generic_field" shortcode doesn't support a "select_text" attribute ( ref: https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field ), but you use the "gettext" filter to change the default select option's text.
( ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext )


add_filter( 'gettext', 'custom_change_default_select_txt', 20, 3 );
function custom_change_default_select_txt( $translated_text, $text, $domain ) {

        switch ( $translated_text ) {

            case '--- not set ---' :

                $translated_text = __( '--- Vyberte tým ---', 'wp-cred' );
                break;
        }

    return $translated_text;
}

Important notes:

1. To get the most accurate and up-to-date information related to text translations using WPML plugin, you can post at its dedicated forum:
https://wpml.org/forums/forum/english-support/

2. Please open a new ticket for each new question or concern, to ensure timely and efficient support.

regards,
Waqar

#1199144

Waqar
Supporter

Languages: English (English )

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

Hi Jiri,

Thank you for sharing the access details.

I noticed that you've added the generic field with the custom shortcode on both forms.

1. The form that adds a new project at:
hidden link

2. The form that edits the existing project at:
hidden link

It would make sense to show only unattached teams on a new project form (1) since you don't want the user to select a team which is not available.

But in the case of edit form (2), you don't need to use the generic field with the custom shortcode. If you'd like a user to assign a different team, even if a connection exists, you can use the regular cred field shortcode:


[cred_field field='@tym-studentu.child' class='form-control' output='bootstrap' select_text='--- not set ---' author='$current']

You can add some text as a warning, that if a new team will be assigned through this edit form, previous connection with the old team will be disconnected.

I hope this makes it more clear.

regards,
Waqar

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