Skip Navigation

[Resolved] Accessing Custom Post Type Data using PHP Session Array

This support ticket is created 5 years, 9 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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by lloydW 5 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1213356

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

#1213659

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

#1213865

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

#1214096

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

#1214192

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