Skip Navigation

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

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.

This topic contains 20 Antworten, has 2 Stimmen.

Last updated by Ljuba vor 3 Jahre, 3 Monate.

Assigned support staff: Nigel.

Author
Artikel
#1120817

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

Now I'm confused, because in 1 and 2 above you say the code works, so what am I supposed to be fixing?

I tried to look at what you have on your demo site.

I was able to log into hidden link, but when I visit an alojamiento post it has a CRED form in the template, and we've been talking about Formidable Forms.

#1121223
06.gif
05.png
04.png
03.png
02.png
01.png

Well,... sorry for mess, lets try as follow:

1) Topic is opened under hidden link and I thought that it was clear, but irrelevant. You have same credentials entrance.

2) Image 01.png shows two snippets. One is (now active) with code from begin of topic (what is wrong with code?) and second is replaced with static post (so, valid only for Hostal Bumba) and you can see how there works well 'repeater' (replace snippets, to try). With code in issue, 'repeater' is 'frozen' (as cannot get posts outside of loop).

3) Image 02.png shows Rooms (two belongs to Hostal Bumba, one for other and one is unrelated).

4) Image 03.png shows available demo hotels.

5) Image 04.png and 005.png shows how active snippet correctly load rooms (with active snippet), depending on hotel page. So, THAT IS WHAT I MEAN UNDER - 'correctly', but form 'repeater' is frozen (cannot repeat fields (only when active snippet is replaced with static post snippet - 'repeater' works - as there is nothing to search vs posts, as it is static).

6) Animated 06.gif shows what I mean under 'not frozen repeater' (now, with active snippet, you cannot trigger it - it is 'frozen').

#1122666

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

The repeater fails which points to a JS, and in the browser console you can see that there is actually a 500 error on the ajax request, meaning that there is a PHP error.

It would surely help to know what that error is, but you have debug logs turned off, so I'm unable to see them.

So I'm taking another copy of your site to update the existing one, and I'll do some debugging locally to see if I can identify the issue.

#1122676

Thanks.

#1123623

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

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

Screenshot 2018-10-09 at 12.41.32.png

I spent some time with this, and within the Formidable filter, on the initial page load you can get the ID of the current post (which is used as the parent post id) from the global $post object, but when using the repeater which triggers the filter during an ajax request, you need to get it via the wp_get_referer() function I described before.

So this code will get it depending on whether you are initially loading the page or adding a repeater via ajax:

add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
function frm_populate_posts($values, $field) {

	if ($field->id == 67) {
		// edit field ID as required

		// Get the parent post id from $post or wp_get_referer
		// depending on whether initial page load or ajax request

		if (wp_doing_ajax()) {

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

		} else {
			global $post;
			$parent_id = $post->ID;
		}

		$relationship = "alojamiento-habitacion";

		$posts = toolset_get_related_posts(
			$parent_id, // 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(), // arguments not required
			'post_object',
			'child'
		);

		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;
}

You can see in the screenshot that it works on my local copy of your site.

#1124027

My issue is resolved now. Thank you!

Wooow...sincerely, speechless. However, I think that Toolset should to somehow publish this solution as I already before mentioned how with Beda we concluded how Toolset most likely never will support in CRED such kind of solution (he would also like to use such features), what is also logical as CRED is not targeted for such purposes.

I can go now with Menu (food orders), Products, Services, Event tickets, etc... HUGE THINK. Thanks again [dot].