Skip Navigation

[Resolved] Get post-ID for a post found in the Relationship form (without hitting Submit)

This thread is resolved. Here is a description of the problem and solution.

Problem:

How can I get the post ID of a post when the visitor selects it from a list of posts?

Solution:

You can try these:

https://toolset.com/forums/topic/get-post-id-for-a-post-found-in-the-relationship-form-without-hitting-submit/#post-1955759

Relevant Documentation:

This support ticket is created 3 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 9 replies, has 3 voices.

Last updated by SigfusB1367 3 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1954065
Relationship-form-vs-View.JPG

Dear support team member,

Is it possible to get the post-ID of the related post BEFORE one hits the Submit button?

The reason I’m asking is that I would like to perform some calculations using the field values of the related post without actually creating the relation (for example for users that are not logged in). For that to work I would need to trigger my calculation shortcode when the user actually selects the post to relate (and somehow be able to get the post-ID).

If not possible, can I do this by using a View? I mean, can I get the post-ID of post when I have narrowed the view down to one post, without hitting the hyperlink to go to that post?

I have added a screenshot to illustrate what I want to do (find the post-ID for the Avocado post!)

And another question:
When I use the Relationship form and AJAX search, the posts that match the letters I write are displayed immediately. When I use a View to search I have to hit Enter or Submit to find the matching posts to my first letters.

Best regards
Sigfús

#1954413

Nigel
Supporter

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

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

Hi Sigfús

I'm not sure I understand what you want to do, or at least when you want to do it.

We are talking about a front-end form, yes?

And a user would use a parent-post selector in the form to choose a parent post, and then you want to perform some calculations based upon fields that are retrieved from the chosen parent post?

This is problematic, if that's the case.

We need to distinguish what is available or generated on the server and what is available in the browser.

You mention using a shortcode to perform calculations, but a shortcode runs (or more precisely is replaced by what is returned by the PHP function that defines the shortcode) when the page is being constructed on the server. Once the page is shipped to the browser, such code has happened, and you can't trigger a shortcode to run later.

Once the form is loaded in the browser, you have JavaScript (or jQuery) available.

So if someone made a selection of a parent field, the selected option would contain the parent post id (that's what you were asking about), and it could be retrieved in JavaScript.

But then what?

You are in the browser, not on the server, so you cannot run PHP to get some field values from that parent post to be used in calculations.

You can, of course, but that requires making an ajax request to the server to run some custom PHP you have written that will return the field values you require so that the JavaScript code can work with them. (You could work with the REST API instead of using ajax, but the principles are the same.) In either case, it would mean custom development, and if it is not something you are able to do yourself you would need a developer.

If you don't have too many parent posts to choose from, then there is another alternative which may be a little bit simpler, although it still requires custom coding both in JavaScript and PHP.

That is, you can use the WordPress function wp_localize_script to pass some data assembled on the server in PHP to the browser, embedded in the page, so that it is available to JavaScript in the browser. (Read more here: hidden link)

If you had hundreds of parent posts it's unlikely you would want to bundle all of that data in the page markup, but if you only had a handful of posts then it would make sense, and is likely easier than using ajax or the REST API.

#1954429

Dear Nigel
Thank you for the very thorough reply. I'll come back tomorrow to better explain what I'm trying to do (which will require javascript coding, as far as I understand from a quick look at your reply. I'll look into it tonight.
So please keek the ticket open until I come back to you.
With best regards
Sigfús

#1955341

Hello,

I have marked this thread as "Waiting for feedback" status, please update here if you still need assistance for it. thanks

#1955607
Recipes-and-related-ingredients.JPG

Dear Nigel or Lou Yang,

Here comes a clearer (hopefully!) explanation of what I’m trying to achieve. I was hoping to save you some time yesterday by keeping it short , but realise now that it became rather cryptic.

The website I’m building has two custom post types, Recipes and Ingredients, with a many-to-many relationship. The Ingredient posts (currently 2000+) hold detailed information about the nutritional values of each ingredients (data coming from a publicly available database at the Swedish Food Authority and some other sources).

For clarification, see the uploaded screenshot (the "Single recipe template" including the related ingredients and the calculated nutritional data for the recipe).

I have created a PHP code snippet that runs through the loop of related ingredients and calculates the nutritional data for the recipe, based on the amount of each ingredient.

So far so good, this all works fine for me in the backend.

I’m working now on how to make this available as a frontend service (i.e. that logged-in visitors can create their own recipes and calculate the nutritional data). So far I have not encountered any major obstacles using the Toolset Forms for this purpose.

The problem I was describing yesterday comes when I try to make this available to visitors who have not created an account. Obviously, I don’t want them to create any new posts and relationships, but I want to offer the “calculator” service. In other words, I’m trying to find out how they can select a number of ingredient and then run the calculation codes and view the output.

The procedure I intend to develop has some similarities to how customers of a WooCommerce site add products to the cart, but this is where I’m currently stuck. I want to present the ingredients posts to the visitor and allow them to “add to the cart” without actually creating posts or relations. If I could find out how I can get the Post IDs of the ingredients posts they select, I would have no problem reading the content of the post to variables, perform the calculation and echo the results.

So, my first question was in essence, - how can I get the post ID of a post when the visitor selects it from a list of posts? I thinks I simply miss something very basic here (I only started with Toolset and PHP some months ago). Can I do this without JavaScript coding by having an “add to recipe” button for each ingredient in the output loop. I would then run my calculator code snippet for all the selected ingredients (temporarily storing the data in arrays/variables) if I conly knew how to get the Post ID for the posts the visitor selected.

(The second question was simply a question of whether the Toolset Search Form could be set up so the matching posts in the view would appear immediately when I start writing the letters, without having to press Enter or hit Submit. Obviously this is possible because this is how it works in the Relationship form. I can do this by installing other plugins, such as Ivory Search, but would rather prefer to stick to Toolset.)

I hope you can help me to cross this hindrance so I can move on from there on my own.

With best regards
Sigfús

#1955675

Thanks for the details.

Toolset Views/Blocks plugins is for display information from your WordPress database, that means if there isn't any relationship between "Recipe" and "Ingredients" posts, then Views/Blocks plugin won't display them as related posts.

And the Woocommerce cart is based on cookies, see their document:
https://docs.woocommerce.com/document/woocommerce-cookies/#section-2
woocommerce_items_in_cart
Helps WooCommerce determine when cart contents/data changes.

But both Views/Blocks plugins do not support this kind of feature, you might consider custom codes, for example:
1) When user click "add to recipe" button of "Ingredients" post, setup the specific cookie value
hidden link
2) Then you can use above cookie value in your custom PHP codes:
hidden link

For your reference.

#1955707

Dear Lou Yang
Thank you very much for your reply. I will study the documents you listed and study how I can make use of cookies and Javascript to move on with my project. That will take some time so we can close this ticket now. I would however like to ask you one final question, if it has an easy answer: How do I add an "add to cart" (or in my case "add to recipe") button to the template for ingredients so I will have a button for each ingredient in the list (output of the view)?
With best regards
Sigfús

#1955713

Just a clarification. I know how to add the button but I meant " a button that provides me the Post ID of the selected post". But if you have stopped working for today I think I can figure it out.

#1955759

You can try these:
1) In views' loop, display a button with post ID as attribute, for example:

<button ingredients-id="[wpv-post-id]">Add to recipe</button>

2) When user click above button, use JS codes to get the button attribute value:
hidden link
hidden link
And setup the specific cookie value

#1955797

My issue is resolved now. Thank you!