Skip Navigation

[Resuelto] Setting post parent with _wpcf_belongs_postNAME_id does not work correctly

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I would like to use Forms to create Foto posts. I have a post relationship set up between Fotos (child) and Fotografos (parent). This is a legacy post relationship that has not been migrated to the new relationships system. Each User is the author of one Fotografo post. I would like to set the parent Fotografo to be the current User's Fotografo post.

Solution: Use a View in the cred_field shortcode to set the _wpcf_belongs_fotografo_id. Set up the View to display the post ID of a Fotografo post, where the author is the same as the current User. Remove the wrapping div from this View. Then you can wrap the cred_field shortcode in a hidden div so it's not visible to the User on the front-end of the site.

<div style="display:none;">
[cred_field field='_wpcf_belongs_fotografo_id' class='form-control' select_text='Choose your photographer' output='bootstrap' type="hidden"  value='[wpv-view name="fotografo-de-este-usuario-en-add-foto"]']
</div>
This support ticket is created hace 5 años, 5 meses. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

This topic contains 8 respuestas, has 2 mensajes.

Last updated by michel-germanQ hace 5 años, 3 meses.

Assisted by: Christian Cox.

Autor
Mensajes
#1153898
4BEA8EAD-CBF2-4A39-9EE1-6DDA61D34792.jpeg

I am trying to: asign a photograph to a post type father (“fotografo”)

Link to a page where the issue can be seen: its backend feature

I expected to see: assign a porhograph with a hidden field in a form, to a post type father called “fotografo”

Instead, I got: the title of the page that contains the form

A moth ago (perhaps something more) it worked correctly but with some update it broke. Please helpme

Regards

#1154003

Hi, when Types 3.0 was released it included a major change to post relationships. The _wpcf_belongs_slug_id custom field method is no longer used to assign post relationships in the new system. If you updated and ran the post relationship migration tool, this could be why the post relationship assignment no longer works. May I ask if you ran the post relationship migration process on your site?

If so, we can remove this field and replace it with another field to assign the post relationship. To do that, I would start by creating a new Form with the same configurations as this Form. In other words, this Form should edit or create new Photograph posts. Then use the Auto-generate button to generate the code automatically. The post relationship field should be created automatically using the updated, correct code. You will copy that code and paste it into your other Form in place of the old cred generic field.

I can help if you have problems.

#1178264
captura_belongs_fotografo_id.jpeg

Hello, the problem was not resolved. I did what you said but the images are not assigned to any photographer.
I explain a little better:

1. There is a WordPress user

2. The WordPress user has a photographer profile created with "types".

3. WP users should be able to add photos to their photographer profile from a creed form with "types" but they are not AUTOMATICALLY assigned to the photographer belonging to their WP profile. On the contrary, there is a select field to select the photographer who owns that photo that we want to upload, but that is NOT WORTH because any user could upload photos to other photographer profiles.

2 months ago you could automatically assign the photographer with the image code but since the update you can no longer

Could you help me?

#1179456

Hi, this ticket was somehow marked resolved and I didn't get notified of your recent post. I have reopened the ticket so we can continue working on this.

On the contrary, there is a select field to select the photographer who owns that photo that we want to upload, but that is NOT WORTH because any user could upload photos to other photographer profiles.
You can use a shortcode to set the value of the new post relationship field automatically, and hide the field, so your Users cannot change it. Or, you can keep the existing generic field and add some custom code that sets the post relationship on the backend when the Form is submitted. Which would you prefer? I can help with either one.

#1189170

Hi,

I prefer to use a shortcode to set the value of the new publication relation field automatically and hide the field.

I've tried with this:

<div class="none">
[cred_field field='_wpcf_belongs_fotografo_id' class='form-control' output='bootstrap' select_text='--- not set ---' author='$current']
</div>

But it dont works. (class="none" means display:none;)

Please help me.

#1189236

I see you are using the "author" attribute and $current:

[cred_field field='_wpcf_belongs_fotografo_id' class='form-control' output='bootstrap' select_text='--- not set ---' author='$current']

The attribute should be "value", not author. The value of the value attribute should be [wpv-post-id] like it was before in the generic field:

[cred_field field='_wpcf_belongs_fotografo_id' class='form-control' output='bootstrap' select_text='--- not set ---' value='[wpv-post-id]']

If this isn't working as expected, I will be glad to log in and take a closer look. Please provide login credentials in the private reply fields here.

#1189714

I just logged in and visited this URL:
hidden link

In the "My photos" tab, I clicked a link to "Add a photo". I am redirected to this URL:
hidden link

There is an error on this page. If you open the console you can see

Uncaught SyntaxError: Unexpected identifier...index:584

If you look at the page source, you can see this code written out:

<script type='text/javascript' class='custom-js'>
add_shortcode('my_parent','my_parent');
function my_parent($atts, $content)
{
global $post;
 
return $post->ID;
}
</script>

There is PHP code written out in the Form's custom JavaScript area, which is causing a JavaScript error. This "my_parent" shortcode definition should be moved out of custom JavaScript and into a custom PHP snippet. I have commented this code out for now.

Next, I can see now that Fotografo is a custom post type. Each User has one Fotografo post, and they are the author of that post. So to relate a Foto to a Fotografo, you need to know the Fotografo ID for the current User's Fotografo post. It looks like there was a View created for this purpose:
hidden link

I edited this View to disable the wrapping div, and inserted the View in the cred_field shortcode:

[cred_field field='_wpcf_belongs_fotografo_id' class='form-control' select_text='Choose your photographer' output='bootstrap' type="hidden"  value='[wpv-view name="fotografo-de-este-usuario-en-add-foto"]']

Now the correct Fotografo is selected (Tomy) when I visit this add-foto page. Can you take a look and let me know if it's working more like you expect now?

#1189736

Thank you, thank you, thank you Christian.
Now everything works perfectly.

I have understood the solution you have proposed and will apply it in future forms.

Thank you very much for your patience and for your way of explaining things.
It's been a pleasure 😉

#1189737

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.