Hi, I am trying to create a wishlist-style function using PHP sessions and the Toolset plugin. Here is the function in essence:
- User clicks a link ("Add to Wishlist") on page and is directed to a Wishlist page
- The URL carries an id, which is stored in a PHP session array
- PHP foreach function cycles round session array, and accesses the corresponding custom post type created using Toolset
- Data from the custom post type is outputted on the page i.e. post title, image, description etc.
So far I have been able to get the ids into the PHP session array, but am struggling to access the custom post type data - can this be done?
You can see a starting page here: hidden link
Password: futuresysceep
When you click any of the "Add to Wishlist" links it takes you to the wishlist page, and the session array is outputted for feedback purposes.
Any help with this would be really appreciated 🙂
Many thanks,
Tom
Hi Tom,
Thank you for contacting us and I'll be happy to assist.
The Types plugin offers "Fields API" which can be used to get custom fields data using either shortcode or PHP function:
https://toolset.com/documentation/customizing-sites-using-php/functions/
Since you already have the IDs of the required posts, you can retrieve the custom fields data from them, using the "item" attribute:
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi Waqar, thank you for coming back to me. Very happy to hear this can be achieved!
Could you guide me further on how to access the custom post type data using a post id? I believe I need to use the numeric ID from the item attribute, but not sure how to access the data itself?
Thanks as ever for great support 🙂
Tom
Hi Tom,
Thanks for writing back.
You'll find a number of examples on the documentation page ( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ ), but this example will also make this more clear.
Suppose, you have a post with ID "100" and you need to get its custom field with the slug "field-slug".
The code, in this case, would be:
$new_val = types_render_field( "field-slug", array( 'item' => 100 ) );
As a result "$new_val" will have the value from that custom field.
regards,
Waqar
Hi Waqar,
Thanks so much for providing these links, I have some basic functionality working - what a powerful tool you've built here 🙂
I'll close the ticket now as I think I can complete this task with the information given.
All the best,
Tom