Skip Navigation

[Resolved] custom field

This support ticket is created 5 years, 10 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)

Tagged: 

This topic contains 14 replies, has 3 voices.

Last updated by martinP-13 5 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1190428

Tell us what you are trying to do?

my post type dog , i have a custom field group name Dogs sire , i have an empty select box and which that box to be either automatically populated or if not possible dynamic list that will let me begin typing a name and then be able to select it.
when the name is found and stored ,iwould like to display it as a link that will then show me all dogs related to that name .

i tried following this https://toolset.com/forums/topic/relationships-between-products-product-to-products/#post-1183541

as reccomended by waqar but upon entering thisin my themes function file i just have empty box with a drop down ,i cannot type in the box.

add_filter( 'wpt_field_options', 'func_to_dynamically_populate_dogs_sire', 10, 3);
function func_to_dynamically_populate_dogs_sire( $options, $title, $type ){
switch( $title ){
case 'Dogs Sire':
$options = array();
// add or generate the array of your desired option titles and values
$options[] = array('#value' => '', '#title' => '---',);
break;
}
return $options;
}

i also tried this code too (changed the required to dogs sired)

add_filter( 'wpt_field_options', 'populate_select_1_1', 10, 3);
function populate_select_1_1( $options, $title, $type ){
switch( $title ){
case 'Person 1 Day 1 - Selection':
$options = array();
$args = array(
'post_type' => 'recipe',
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'recipe-category',
'field' => 'slug', // term_id, slug or name
'terms' => 'this-weeks-recipes'
)
)
);
$posts_array = get_posts( $args );
foreach ($posts_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}

break;
}
return $options;
}

thank you ever so much, how can i reasine to a different helper? i cant wait until tomorow as i need to complete today!

#1190432

Hi Martin,

Thanks for asking! I'd be happy to help.

I'm currently working on this ticket and please expect the reply shortly.

Note: I'm around for another hour today, so you're welcome to reply back on this thread for related questions after that. But If you'd like to put your (related or a new) question to the next available supporter while I'm away till tomorrow, you can always open a new thread.
(just add a note that it is related to the other ticket and the other team member will understand)

regards,
Waqar

#1190447

Hi Martin,

Thank you for waiting.

I noticed that you've added the function to automatically generate the options for the select field "Dogs Sire", but that function wasn't calling the dog post type items.

To do that you can use "get_posts" function.
( ref: https://codex.wordpress.org/Template_Tags/get_posts )

For example:


add_filter( 'wpt_field_options', 'func_to_dynamically_populate_dogs_sire', 10, 3);
function func_to_dynamically_populate_dogs_sire( $options, $title, $type ){
	switch( $title ){
		case 'Dogs Sire':
			$options = array();

			// add first empty value
			$options[] = array('#value' => '', '#title' => '---');

			// get all dog item posts
			$args = array( 'post_type' => 'dogs', 'posts_per_page' => -1, 'post_status' => 'publish','orderby' => 'title' );

			$results = get_posts( $args );
			if ( $results ) {
				foreach ( $results as $post ) {
					$options[] = array('#value' => $post->ID, '#title' => $post->post_title);
				}
			}

		break;
	}
	return $options;
}

This should generate the list of options for the field and when you'll select a dog and save, that dog post's ID will be saved in that custom field.

I hope this helps.

regards,
Waqar

#1190453

thanks waqar , it breaks site and gives me a console error event the home page
hidden link

#1190562

Hi, I'll be glad to take a closer look. Please provide login credentials in the private reply fields here.

#1190648

I must not understand completely, sorry. I copied Waqar's code and pasted it at the bottom of your current theme's functions.php file. Then I visited the homepage successfully and I don't see any error messages or console errors. I also opened the Dog editor page in wp-admin and I can see the Dogs Sire select field populated with some options. No obvious errors here either. Can you give me some additional details about the problem? Maybe there was a typo or transposition error during the copy + paste process before?

#1190662

which file did you place waqas's code? i dont see it in /public_html/wp-content/themes/simply-responsive-cp/functions.php ?

here hidden link i set gary sire to "harry"
in my /public_html/wp-content/themes/simply-responsive-cp/single-dog.php viewed here hidden link i dont see harry populated
in my /public_html/wp-content/themes/simply-responsive-cp/tribe-events/single-event.php viewed here hidden link i see harry entered next to sire but it is not clickable.
i think i've got myself tangled up now.....

#1190694

i now see the code at bottom of functions..... rest of above though still , and is there a way so the list populates when i start typing as when i start entering my content that list of sire will be over 1000!!

#1190712
Screen Shot 2019-01-24 at 2.39.40 PM.png

which file did you place waqas's code? i dont see it in /public_html/wp-content/themes/simply-responsive-cp/functions.php ?
It's there at the bottom of the file. Screenshot attached. Perhaps you did not download the latest version to your local repository?

in my /public_html/wp-content/themes/simply-responsive-cp/single-dog.php viewed here...i dont see harry populated
Right because your code used the post relationship syntax unnecessarily:

Sire : <?php echo do_shortcode( "[types field='dog-sire' item='@events-dog.child'][/types]");?></strong><br>

You don't need the item attribute here because you're already on the single Dog page. So there's no need for referencing another related post using the @events-dog.child item syntax. I've removed that for you, and I can see "Harry" now. Be sure to pull that update into your local repository.

in my /public_html/wp-content/themes/simply-responsive-cp/tribe-events/single-event.php viewed here...i see harry entered next to sire but it is not clickable.
Okay that's coming from this View, and doesn't really have anything to do with the theme files:
[wpv-view name="view-to-show-all-dogs-related-to-an-event"]
hidden link
The code in this View isn't set up to show a link to the sire post, only to show the information collected in the Sire field:

Sire : [types field='dog-sire' item='@events-dog.child'][/types]<br>

To show a link to the sire's post, you need to combine the ID from the sire field with the wpv-post-link tag. I've made that change for you:

Sire : [wpv-post-link id="[types field='dog-sire' item='@events-dog.child' output='raw'][/types]"]<br>

The output='raw' attribute tells the dog-sire field to return the sire's ID instead of the name. Then we pass that ID into the wpv-post-link shortcode. You can see that some of the information shown now for Sires is incorrect. I assume this is because the sire information has not been entered yet? If that's the case, and you need to hide this information when Sire is blank, you need a conditional. We can work on that in a separate ticket if necessary.
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#1190770

thank you christian ,does this mean i must have already created the dog before i can enter it under sire in the dog in events ? ie here hidden link
is there no way to create the new dog there? (im guessing not), am i setting this up the best way? or is there a better way you know of?
thankyou

#1190779

thank you christian ,does this mean i must have already created the dog before i can enter it under sire in the dog in events ?
Yes, that is correct. Your workflow should involve creating the older Dogs first, and proceeding down by generation.

is there no way to create the new dog there?
No, there is not a way to modify custom field options directly in the post editor screen, or publish other Dog posts ad hoc while creating another Dog post.

am i setting this up the best way? or is there a better way you know of?
I think what you have started already is the best way. The other options for connecting posts in Toolset are:
- Post relationships
- Custom taxonomy terms
Post relationships are not possible here because you cannot relate posts in the same post type. Toolset's post relationships system does not support same-type post relationships. Custom taxonomy terms might be possible, but it would be even more complicated than the approach you have here. You would have to use term slugs and post slugs creatively to link Sire and Dam posts to their offspring. No, I think you have the best option here already.

#1190780

many thanks christian for your time and insights. i will continue with this way!

#1190792

Okay sure. My day is ending so if you need immediate help please feel free to submit a new ticket as you did before, or you can post your follow up here and I will respond Sunday when I return.

#1190831

everything above is good except here hidden link Sire : Phil test sire 1 is not a link. (i think i must change the shortcode in/public_html/wp-content/themes/simply-responsive-cp/single-dogs.php ?

this is a custom field under the post type dogs , i have identical except "dog sire" it is "dog dam" . i copied all the info from this help but on single-dogs post (single-dogs.php) i get no output and on single event the dam is displayed even though i have added dams . im at a loss !! even copying a working function doesn't lead to a working copy....

#1190951

My issue is resolved now. Thank you!