Skip Navigation

[Resolved] Post relationship and intermediary post

This support ticket is created 3 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
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)

Author
Posts
#2109369

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pedro

If first option is doable, how would they be set, would be something like this?
Course ---- 1-M ----> Instance ---- M-M ----> Registration

I'm assuming you're referring to this question above, if so then yes this is the correct way to format your relationships as per my first approach.

Thanks,
Shane

#2114479

Hi,

I was able to create an instance of a course (location/date) and when a user fills the credform registration it assigns it thew a relationship field automatically to the current location in the loop. This was possible using a custom shortcode and a generic field:

add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
    if ($form_data['id']==1347) {
            
        toolset_connect_posts('localizacao-inscricao',$_POST['@localizacao-inscricao.parent'], $post_id);
    }
}

[cred_generic_field field='@localizacao-inscricao.parent' type='text' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-id]"
}
[/cred_generic_field]

Regarding point 2 of the original question, i need to set a condition on a button to check:

If field "Registration Limit" of the current location/date cpt in the loop (this is a normal field of the cpt) <= current related posts then {
etc.. etc..
}

And this is what i'm having issues with. I ran the conditional wizard and i get this:

[wpv-conditional if="( $(wpcf-limite-de-inscricoes).item(@localizacao-inscricao.parent) lt '' )"]

but it's missing the second part which is how to get the number of registrations that were already created in that location. This is for course location, so i create "Webdesign Paris" and set the field registration limit to 10. If i already have 10 registrations assigned to that location (1-M relation) and the limit is 10, i want to display a SOLD badge.
I checked the Post relationships API here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/ and i see "toolset_get_related_post_types" with the Description "Get post types related to the provided one.", but i wish to get the count... not the list.

How can i achieve this? If needed access please place answer as private.

thanks

#2114625

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pedro,

The function you should use is this one below.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

What it does is that it will return the list of related posts. Now we don't have a function that returns the count, however you can use the count() php function on this list to get a number value of how many posts are in the relationship.
hidden link

Please let me know if this helps.
Thanks,
Shane

#2115175

Thanks Shane,

I added to functions.php:

add_shortcode('registrations-count', 'registrations_count_func');
function registrations_count_func($atts) {
  global $post;
   
  $child_posts = toolset_get_related_posts($post->ID,'localizacao-inscricao','parent',999,0,array(),'post_id','child');
  return count($child_posts);  
}

And went to Toolset settings and registered registrations-count as shortcode to use in Views.
I posted the shortcode into the view to see if i would get a number and all i see is [registrations-count] shortcode in pure text... so it's not rendering the shortcode. I also tried adding {!{registrations-count}!} and the problem remains the same...

What am i doing wrong?

The site is using Avada theme.

Thanks

#2115195

Nevermind... the code was in functions.php of the child theme that wasn't activated. It's rendering and apparently the number is correct, if I need any more help I will post.

thank you once again

#2115227

Well it seems that point 1 is no longer working, although the CPT is created when cred form is submitted there is no assigment of the relationship. An error is returned:

Notice: Undefined index: @localizacao-inscricao.parent in function.php
Fatal error: Uncaught InvalidArgumentException: The parent must be a post ID or a WP_Post instance.

So this line is incorrect:

toolset_connect_posts('localizacao-inscricao',$_POST['@localizacao-inscricao.parent'], $post_id);

The form is located in the course page just below each location (which is a looped view) and the ID is 1333. The generic hidden field has name="@localizacao-inscricao.parent" and i can see it displays the number 1334 which is the ID of the instance (location/date related to the course) for which we are registering to.

[cred_generic_field field='@localizacao-inscricao.parent' type='text' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-id]"
}
[/cred_generic_field]

So what is wrong?

#2115369

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pedro,

The problem here "@localizacao-inscricao.parent"

I would suggest checking the payload of the page by inspecting the POST request when the form is being submitted.

Using the Chrome inspection tool, Navigate to Network tab and then click on preserve log. From there submit your form as you would then check the POST request for the name being passed.

If you're unsure of how to do this then you can provide me with access tot he form so that I can submit the form as a test to see what the name is that is showing up for this field.

Thanks,
Shane

#2115383

Shane,

we could post a screenshot of the post request but we dont wish to disclosure the domain. i could provide the URL but the site is in maintenance mode which means you must login to have access.
We are more confortable providing access to admin and you see it for yourself.
Could you please set next post as private?

thank you.

#2115585

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pedro,

Yes provide the screenshot for me or a link to the form where I can do a test submit, I've enabled the private fields.

Also you can try getting the value directly from the payload using $_REQUEST.

Thanks,
Shane

#2118947

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pedro,

Thanks, can you let me know where the code was added, I would like to do some debugging to see if I can understand what is happening here.

It may be a case where the parent value isn't being accessed in the code. What you can do to check this is to dump the variable onto the page itself to see what exactly is happening.

However please let me know where I can access the code.

Thanks,
Shane

#2118987

Sorry Shane,

Don't know why Avada doesnt show the Theme Editor.
Anyway, i installed File Manager where you can access the Avada child-theme functions.php where the code was inserted.

Out of curiosity, how would i dump the variable onto the page itself?

#2119023

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pedro,

Sure what I do is to dump the variable using this function below.

           var_dump( $_POST);

Which when the form was submitted it gave me the results below.

array(7) { ["form_submit_1"]=> string(6) "Submit" ["@localizacao-inscricao_parent"]=> string(4) "1334" ["_cred_cred_wpnonce_cred_form_1347"]=> string(10) "606011d1f6" ["_cred_cred_prefix_post_id"]=> string(4) "1370" ["_cred_cred_prefix_cred_container_id"]=> string(4) "1334" ["_cred_cred_prefix_form_id"]=> string(4) "1347" ["_cred_cred_prefix_form_count"]=> string(1) "1" }

As you can see the index being used for the parent is "@localizacao-inscricao_parent" so in your code you should change @localizacao-inscricao.parent to @localizacao-inscricao_parent

This should resolve the issue of the unidentified index. I will leave the var_dump() function active in your code so that you can have a look at what I did to debug.

Thanks,
Shane

#2120051

Shane,

Thank you for the feedback.
Yes that worked out fine. I'll leave this thread open because I might need some help displaying the list of courses and their locations, if that's the case I will post here.

Thank you once again.

#2131805

Hi, just checking in to see if you were able to get the list of locations and courses working as expected. If so, I think we can close out here.