Skip Navigation

[Resolved] What is wrong in function? ($parent_post = get_the_ID();//Parent)

This support ticket is created 6 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 20 replies, has 2 voices.

Last updated by Ljuba 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1114077

Tell us what you are trying to do?

==> I created with Formidable Forms (as it can't be done with Toolset) 'Booking form' and there is the CPT data to be 'pulled out' from the post where is form inserted ('hosted'). Original code is by Formidable and related to posts, but modified by Beda to 'pull out data' from posts. It is with 'static' post ID and in that case, all works fine ('works fine' is about two elements/functionalities > 'pulling data' and 'repeating the form field set'). But when

$parent_post = '198';//Parent

is replaced with

$parent_post = get_the_ID();//Parent

Form correctly assign itself and 'pull the data' in the fields, but it can't to be repeated, as when a repeater is added, it is done with ajax. During an ajax call, it is no longer inside the WordPress loop, and there is no post to get.

So, what is the correct way to make it to work?

Entire code is

add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
function frm_populate_posts($values, $field){
  if($field->id == 67){ //replace 67 with the ID of the field to populate  
	  $parent_post = get_the_ID();//Parent
	  $relationship = "alojamiento-habitacion";
		$posts = toolset_get_related_posts(
		$parent_post, // get posts related to this one
		$relationship, // relationship between the posts
		'parent', // get posts where $parent_post is the parent in given relationship
		999, 0, // pagination
		array(), // How was his surname, again…?
		'post_object',
		'child'
		);
	//get_posts( array('post_type' => 'post', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
    unset($values['options']);
    $values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
	$values['options'][''] = '';
    foreach($posts as $p){
      $values['options'][$p->ID] = $p->post_title;
    }
    $values['use_key'] = true; //this will set the field to save the post ID instead of post title
	unset($values['options'][0]);
  }
  return $values;
}
#1114425

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I didn't look too closely at that code, let me just suggest that you replace

$parent_post = get_the_ID();//Parent

with

global $post;
$parent_post = $post->ID; //Parent

If that doesn't work I'll need to take a closer look.

#1114482

No, not working. Same behavior.

#1114995

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

frm_setup_new_fields_vars is a Formidable Forms filter?

And these "repeaters" that update via ajax are part of the Formidable form?

Sorry, I think you need to ask Formidable about how you can access the post object for the post where the form is inserted within its own ajax calls originated by the form.

I looked at the documentation for that filter and it is not available as an argument, but they should know if it is accessible another way.

#1115180

Well, unfortunately, that is exactly the reason why I posted for help here. This is typical situation where in combinations of two plugins, both authors claim lack of 'jurisdiction'. And I will try now just to (avoid the arguing, rather to) explain why I believe how problem is within the part of the code provided by Toolset support. However, I also don't claim how code is wrong, as it works with 'static' post ID. I simply ask where could be the problem (and it is within indicated part).

So, original code provided by Formidable (hidden link) is

add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_posts', 20, 2); //use this function on edit too
function frm_populate_posts($values, $field){
  if($field->id == 125){ //replace 125 with the ID of the field to populate
    $posts = get_posts( array('post_type' => 'post', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
    unset($values['options']);
    $values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
    $values['options'][''] = ''; //remove this line if you are using a checkbox or radio button field
    foreach($posts as $p){
      $values['options'][$p->ID] = $p->post_title;
    }
    $values['use_key'] = true; //this will set the field to save the post ID instead of post title
    unset($values['options'][0]);
  }
  return $values;
}

and this is the Formidable support reply
"My first response explains why get_the_ID() will not work during an ajax call. If you would like further detail and explanation on using loop values during an ajax call, you will need to hire a developer who can help with this."

So, as get_the_ID() is the (initial) part of the my question (previous topic) - How to pull out the data from the fields from custom post type? - I believe how that is the part of the Toolset support 'jurisdiction'.

However, if you will decide to keep with standing how it is not under Toolset support 'jurisdiction', I will not argue (further).

P.S. - ESSENCE

In (again) other and still opened topic, we (me and Toolset support) found how unfortunately, Toolset can't be used in case where it is needed to repeat set of data (so, partially also part of RFG lack of features), but I found the way to do it trough combination of Toolset and Formidable. So, from my point of view, it will be also benefit of Toolset if this topic will be successfully solved (a part of 'code jurisdiction'), as I believe how Toolset support should to get 'entire picture' of real dimension and meaning for Toolset (significantly 'expanded' possibilities of Toolset 'application' - to not start now to name situations in 'real life').

Briefly, Toolset invested lot of resources to develop RFG, but it is now proven how Toolset is not capable to effectively apply it in some very frequent and common 'real life' situations (and probably - never will be capable - should be read other topic).

I found solution for that situations, but .... if Toolset will reject 'code jurisdiction', I reached my (knowledge and resources) limits and .....

In other words, Toolset should to 'face itself' and ask itself, what was the factual purpose of RFG development if there will be NEVER answers (solution) for many very common 'real life' situations (RFG is for 'marketing purpose' only???).

#1116172

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2018-09-27 at 09.19.48.png

Hi Ljuba

When you talk of repeaters on your Formidable form, is this what you are referring to? hidden link

They have a test example on their own page.

In the screenshot you can see where I added some additional field groups, each of which created an ajax request, which I presume triggers the frm_setup_new_fields_vars filter you use in your code examples.

If this is what we are talking about above, and the global $post object inside that filter does not refer to the post where the form is inserted, then it seems entirely reasonable to ask Formidable support (and expect an answer) how you retrieve the ID of the post where the form is inserted.

The people best placed to answer that question surely are Formidable because it can depend on how their plugin is coded.

I would ask again and don't give code samples or anything else that distracts from the key point, which is "how do you retrieve the ID of the post where the form is inserted when inside the frm_setup_new_fields_vars filter".

#1116244

Yes, it is about it.

Unfortunately, problem is proven not on Formidable. You can see and try it on my website (you have credentials). In snippets are added 'static' option (198) and 'get_the_ID();' option. So, (booking) form is inserted on 'Alojamiento' Content Template and if I set specific post ID (198) EVERYTHING WORKS PERFECT. As it cannot be specific (static) ID as it is template, I need something how I get it. If I use 'get_the_ID();' (or your suggestion or some other options too) - IT PULL DATA CORRECTLY, but fail on 'repeater' (ajax).

In other words, despite that Formidable support indicated as wrong 'get_the_ID();', for me, all indicate that it is something wrong in the rest of the 'Toolset code' (code provided by Beda), or in other words, something stop ajax to be 'triggered' (if static post ID is not used).

Formidable support answer is:

"When a repeater is added, it is done with ajax. During an ajax call, you are no longer inside the WordPress loop, and there is no post to get. So... I'm afraid you'll need to come up with a different way of doing this."

As previously displayed (original Formidable) code retrieve exactly post ID and here is about the specific data from specific custom fields (so. provided by Toolset), Formidable support obviously will not help.

Briefly, I tried to simplify topic wit title (as something obviously already works), but here title instead should to be ==>

How to pull out data from specific custom fields, to external tool (form) executed (trough ajax) outside of the WordPress loop?

#1116252

I found that it (probably) should to be used something like

global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();

but, question is - how exactly should to be modified the code?

#1116340

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Ljuba

I don't want to get into a fight with their support, but their answer is a cop out.

That filter runs whenever you add a repeater section via an ajax request.

It is entirely possible you would want to know the ID of post where the form was displayed, even if you didn't have Toolset installed, and this really has nothing to do with Toolset and should be handled by Formidable.

In any case, here is how you can get the ID of the current post where the form is inserted, to then use however you wish, in your case for connecting posts.

add_filter('frm_setup_new_fields_vars', 'frm_test_api', 20, 3);
function frm_test_api( $values, $field, $args ){

	$url = wp_get_referer();
	$post_id = url_to_postid( $url );
	// ^^ this is the ID of the post where the form is inserted

	return $values;
}
#1116699

Hi NIgel,

1) I agree about the comment related to Formidable support answer.

2) I tried

add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
function frm_populate_posts($values, $field, $args){
  if($field->id == 67){ //replace 67 with the ID of the field to populate 
		$url = wp_get_referer();
    		$post_id = url_to_postid( $url );
	  	$relationship = "alojamiento-habitacion";
		$posts = toolset_get_related_posts(
		$parent_post, // get posts related to this one
		$relationship, // relationship between the posts
		'parent', // get posts where $parent_post is the parent in given relationship
		999, 0, // pagination
		array(), // How was his surname, again…?
		'post_object',
		'child'
		);
	//get_posts( array('post_type' => 'post', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
    unset($values['options']);
    $values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
	$values['options'][''] = '';
    foreach($posts as $p){
      $values['options'][$p->ID] = $p->post_title;
    }
    $values['use_key'] = true; //this will set the field to save the post ID instead of post title
	unset($values['options'][0]);
  }
  return $values;
}

and it break the Toolset template.

3) Here is nothing to fight about it. I will accept as complete legitimate if you will reply how this is out of scope of Toolset support. I only want to repeat, how solution of this issue is in Toolset BEST INTERESTS, as it is very likely how Toolset NEVER WILL have such option (repeating set of the fields), what makes entire Toolset effort to implement RFG, quite useless (at least, it is clear outcome in topic https://toolset.com/forums/topic/content-less-form-with-repeatable-field-groups/page/3/#post-1115523).

In other words, it is entirely on you (mean - Toolset support, not personally).

I found solution, but for some reasons, I can't get it to work. However, I'm not capable to solve the issue.

#1117239

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, this week has been really busy, I didn't get chance to look at the code, I will on Monday.

#1118335

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Ljuba

Just looking at this code there is an obvious problem.

You are using toolset_get_related_posts and the first argument is $parent_post, but you don't specify what $parent_post is (surely there is a warning in the PHP logs about this?).

Where I have shown you how to get the ID of the post where the form is displayed, using

        $url = wp_get_referer();
        $post_id = url_to_postid( $url );

I think that $post_id is what you want to use for $parent_post, no?

#1118580

Fact is how Form is placed on Alojamiento post template and field from where is pulling data is from Alojamiento child post (Habitaciones).

To be honest, I don't have idea what to use, as I'm not familiar with this. I can see how it not looks logical, but how it should to be - .... (that's why title is as it is - what is wrong?)?

#1119924

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, let's go back to the beginning, because, while I have been able to spot some specific problems with the code, I'm not actually sure what the code is supposed to be doing.

You are on an alojamiento page.

You have a form inserted on that page.

The code seems to be about updating a field in the form with options that are built from habitaciones child posts.

You want that the form should display a dropdown that lists existing habitacion child posts connected to the current alojamiento?

What is the purpose of the form?

What is the purpose of this select dropdown field?

#1120079

1) "The code seems to be about updating a field in the form with options that are built from habitaciones child posts."

Exactly. Form field 'Seleccione la habitación' is filled by Post title of Alojamiento child posts Habitaciones. On site are two Alojamientos demo posts and one have two rooms and one have one room. Existing code, correctly display related child posts accordingly relationship.

2) "You want that the form should display a dropdown that lists existing habitacion child posts connected to the current alojamiento?"

Answered above (yes, and it works correctly).

3) "What is the purpose of the form?"

==> Booking

==> I must to say that I want to make 100% same form for Restaurants Food Order, for Services and for Products.

4) "What is the purpose of this select dropdown field?"

In Habitaciones post I have field 'Tipo de habitación' and it's open me option to have form without such 'dynamic populated form', but there is obvious how much better is if I will be able in that dropdown to offer ONLY EXISTING ROOMS, instead of room types (and now extend view on existing food for restaurants, existing services for service business, ...).

In other words, it enable that people can order Pizza only if Pizza exists as the option in restaurant menu, as well to Booking only (ie) Dormitory if dormitory exists in hotel. If there is no dormitory, dormitory will not be listed as option in Booking form.

It is well visible if you will open both frontend pages of both demo hotels (different rooms are in dropdown listed).