Skip Navigation

[Resolved] I would like one field to accept 2 values for one instance

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

Problem:
How to query and dispaly child posts with types post relationsip

Solution:
You can use types function "types_child_posts()" to get child posts of given parent.

You can find the proposed solution with the following reply:
https://toolset.com/forums/topic/i-would-like-one-field-to-accept-2-values-for-one-instance/#post-393656

Relevant Documentation:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

This support ticket is created 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by davidG-5 7 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#392461

Good day,

I would like to create a colour palette generator for my paint website. I would like to be able to insert both a "Colour Name" and a "Hex Code" for each colour of a specific product.

For Example:
"Wall Paint" would have a Single Line "Colour" Field that accepts multiple-instances.
Each entry would look like this: Blue, #88d0e8.
Add Another Instance: Orange, #f5983b
Add Another Instance: White, #ffffff
So Basically, each colour would have one entry containing two values separated by a comma.

I need to call the "Colour Name" and "Hex Code" separately as I'd like to add the "Hex Code" into CSS and I'd like to display the "Colour Name" on the front end.

I would prefer both values to be entered into the same field as each product will have many different colours and I'd like to avoid confusion.

Please advise if and how this is possible?

Thanking you in advance 🙂

#392513

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Types color picker field accepts the only hexadecimal value and validates it, so to add the color name is not possible.

I think you should go with the group of repeating fields. The following link will guide you to achieve this.
https://toolset.com/documentation/user-guides/creating-groups-of-repeating-fields-using-fields-tables/

#392568
types-product-edit-3.png
types-product-edit-2.png
types-product-edit-1.png
types-field-groups.png
types-custom-fields-group.png
types-cpt-edit-4.png
types-cpt-edit-3.png
types-cpt-edit-2.png
types-cpt-edit-1.png

Thanks Minesh, much appreciated.

I am trying to do as the documentation says as it seems to be exactly what I am looking for, but the "Post Relationship" part is not displaying the way the documentation suggests.

I understand that I may be doing things incorrectly, please view my screenshots and advise?

Thank you.

#392845

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please tell me Types plugin version you are using?

#393250

I am using Version 1.8.11, I will update and see if this solves my problem.

#393259

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - Please let me know the results. I hope with update it will work.

#393384

Hi Minesh,

Thank you for the help so far.

I updated my plugin and found things to be as the documentation suggested, so I got my field group to repeat as I needed it to.

I am now having an issue displaying the colours, my code looks like this:

<h3>Colours</h3>

                        <?php if (have_posts('colours')) : while (have_posts('colours')) : the_post(); ?>
                         
                        <?php $product = get_the_ID(); ?>
                        
                        <?php $args = array(
							'posts_per_page'   => -1,
							'post_type'        => 'colours',
							'post_parent'      =>  $product,
							'post_status'      => 'publish'
                        );
						
                        $colours = get_posts( $args ); ?>
                        
							<?php if(types_render_field("colour-name") !== '') { ?>
                            
								<?php 
								
									foreach($colours as $colour):
								
										$standard_colour_name = types_render_field("colour-name", array("raw"=>"true","separator"=>";"));
										$standard_colour_code = types_render_field("colour-code", array("raw"=>"true","separator"=>";"));
										
										 echo $standard_colour_name;
										 echo $standard_colour_code; 
									 
									endforeach;
									
								?>
                            
							<?php } else {
                                echo '';
                            } ?>
                            
                        <?php endwhile; endif; ?>

Any help would be greatly appreciated,

Thank you 🙂

#393656

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you use PHP API for fetching child post:

For example, to load ‘room’ child items:

	
$child_posts = types_child_posts("room");
foreach ($child_posts as $child_post) {
  echo $child_post->post_title;
  echo $child_post->fields['description'];
}

If you need more control of the returned posts you can create a query directly. This example returns all rooms that belong to the current property and will sort them by the description field.

	
$childargs = array(
'post_type' => 'room',
'numberposts' => -1,
'meta_key' => 'wpcf-description',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_property_id', 'value' => get_the_ID()))
);
$child_posts = get_posts($childargs);

More info:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

#397527

Thank you. This solved my issue.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.