Skip Navigation

[Gelöst] Limit the relationship list in a form

This support ticket is created vor 5 Jahren, 4 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 6 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Pat vor 5 Jahren, 4 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1270819

Pat

Hello,

I have defined a relationship and placed it inside a Cred form (product-inscription).
No issue there, I'm getting the list of products.
Now, I would like to limit the list of products proposed by the relationship field. For example, I would like to have only post with the custom date field "date-debut" greater than TODAY.

How can I do this?

Regards
Pat

#1271013

Hi, there's no built-in filter for a post relationship field in Forms. One workaround is to create a generic select field and use a filtered View to create the options for the field. Then use the cred_save_data hook to programmatically associate the new player's posts using the Forms and Relationships APIs.

I can give you some code examples of those steps if you'd like.

#1271063

Pat

Hi Christian,

Thanks for the info.
Yes, I would appreciate to have some code from your side.

Here is my current field :

<div class="form-group">
<label>Choix du stage</label>
[cred_field field='@produit-inscription.parent' class='form-control' output='bootstrap' select_text='----Choix du stage----']
</div>

And then, the custom date field (integrated inside the product postype) : 'date-debut-stage' that need to be sorted (only the fields with a 'date-debut-stage' > TODAY should be proposed int he dropdown select box).

Regards
Pat

#1271079

Okay first you need to create a View of Products, filtered by the date-debut-stage field, where the value is an unsigned greater than TODAY(). Check the checkbox to disable the wrapping div. This View must not use pagination or AJAX updates. After you verify the View is returning the correct results, use this code in the Loop Output:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1]{"value":"[wpv-post-id]","label":"[wpv-post-title]"}[wpv-item index=other],{"value":"[wpv-post-id]","label":"[wpv-post-title]"}</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

Then create a generic select field in your Form and use the View to generate the options. Here is an example:

[cred_generic_field type='select' field='my-generic-select']
{
"required":0,
"options":[ [wpv-view name="my-post-list"] ]
}
[/cred_generic_field]

Here's how you access the selected option value in a cred_save_data hook:

add_action('cred_save_data', 'my_cred_callback',100,2);
function my_cred_callback($post_id, $form_data) {
  $val = $_POST['my-generic-select'];
  // now $val holds the selected post ID and $post_id holds the new post's ID.
}

Here's how you can connect two posts using the toolset_connect_posts API:

toolset_connect_posts( 'relationship-slug', $parent_id, $child_id );
#1271541

Pat

Hi Christian,
Sounds good.

I had already created a Views to select the right product and now, have integrated it inside the cred_generic field value.
Now, that being said, I can select the right product thanks to the generic field, but I need to record the relationship inside the product and the inscription.

So, if I understand well, I need to use the :
toolset_connect_posts( 'relationship-slug', $parent_id, $child_id );
and insert it in a cred_save_data linked to the form?

My relationship slug is 'produit-inscription'
(meaning a produit could have several inscriptions). How do I need to affect $parent_id and $child_id between 'produit' and 'inscription'?

Second question : in the same cred_save_data, I need to create the title and in the title, I need to retrieve some info concerning the relationship (ie : the custom field 'lieu-du-stage' that belongs to the 'produit').
Is it feasible and could it be done in the same cred_save_data (meaning : the information concerning the relationship are already recorded) or do I need to create another cred_save_data with a lower priority to do it?

Regards
Pat

#1271865

How do I need to affect $parent_id and $child_id between 'produit' and 'inscription'?
Parent ID is in the $_POST superglobal. Look for the slug of your generic field as the key. Child ID is the ID of the post that was just created. You have access to that in the callback function as $post_id.

Is it feasible and could it be done in the same cred_save_data
Yes, it's feasible. You can use wp_update_post to change the new post title or whatever.
https://codex.wordpress.org/Function_Reference/wp_update_post

#1274101

Pat

Hi Christian,

Thanks for your help.
Everything is fine now.
Regards
Pat