Skip Navigation

[Resolved] Custom Field (Post Reference) default value

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

Problem:
Custom Field (Post Reference) default value

Solution:
Currently we do not support self-join relationships that means you should not use post reference field to connect same post type.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/custom-field-post-reference-default-value/#post-908842

Relevant Documentation:

This support ticket is created 6 years, 7 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 12 replies, has 2 voices.

Last updated by WillL606 6 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#907813
Screen Shot 2018-06-02 at 3.12.54 PM.png
Screen Shot 2018-06-02 at 3.13.42 PM.png
Screen Shot 2018-06-02 at 3.13.52 PM.png
Screen Shot 2018-06-02 at 3.14.07 PM.png

I am trying to:
Remove the default value in setting up a Post Reference Custom Field. The field should offer a dropdown/searchable list of essays, but the default for the field enters, for some reason, the essay's category. I have a conditional to only display the field of reference is added to the field. The problem is that if I don't fill the field, the default (the category) gets activated, and I get a 500 error (because, the default category is not an essay!)

Link to a page where the issue can be seen:
hidden link
This is happening on the backend - see images below.

I expected to see:
No default set inside custom field

Instead, I got:
A category as default entry of Post Reference

#908358

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - the thing is that currently we do not support self-join relationships that means you should not use post reference field to connect same post type post and there was a bug with the RC plugins you were using that made it possible but we already fixed that bug and you will not be allowed to use post reference field to connect same post type.

Additionally, After checking debug information you shared with us, I found that you are using outdated Toolset RC plugins. We always recommend to run your site with latest stable release plugin version.

*** Please make a FULL BACKUP of your database and website.***
Could you please update ALL Toolset plugins to it's latest official released version. You can download latest plugin release from your accounts page:
=> https://toolset.com/account/downloads/

We have plans to implement self-join relationship that will be implemented in near future release but you need to wait for that. There is no ETA on it.

#908452

Minesh,
Do you have any suggestion for how to show a link that connects one post to another of the same post type? Something that will be stable over time? Should I create a relationship?

W

#908454

I just updated the plugins and everything still works AND the default value has gone away. Problem solved?

#908842

Minesh
Supporter

Languages: English (English )

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

Do you have any suggestion for how to show a link that connects one post to another of the same post type? Something that will be stable over time? Should I create a relationship?
==> as I try to explain - self-join relationship is not yet supported for now but our Devs have plan to implement it and its on roadmap but when it will be available for public use is not yet decided as this feature is still under assessment. There is no ETA on it.

I just updated the plugins and everything still works AND the default value has gone away. Problem solved?
==> No - you should not use it even though it works for you for now because - it will give you pain sooner or later when it stop working as this is not the way to use that field - I would suggest to remove that field completely from your custom field group.

I think I have a workaround to suggest - You should add a dropdown select field to your custom field group and fill its value dynamically.

Once you created the field - then you should use following hook to populate the dropdown select options dynamically.

For example - I've created custom field "select essasy" with your custom field group and then added following code to your current theme's functions.php file that is used to populate the dropdown with existing essay post type entries :

add_filter( 'wpt_field_options', 'func_dynamic_populate_essey', 10, 3);
function func_dynamic_populate_essey( $options, $title, $type ){
    switch( $title ){
        case 'select essasy':
            $options = array();
            $args = array(
                'post_type' => 'essay',
				'numberposts' => -1,
                'post_status' => 'publish');
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

If you add/edit the essay post - you will see the "select essasy" is available to select the options and you can save the ID of the selected option with custom field "select essasy".

#909096

Minesh,
Thanks for the work around. But I have two remaining questions:
1) I tried to change the naming of the custom field to "riPOSTe to" instead of "select essasy" and failed to replicate the drop down menu of essays. Of course, I made sure to replace the "case" in the hook to reflect that name change, but I guess I missed some other change in the function. If I want to rename the custom field, what should I change?
2) In my attempt to insert this custom field value into an essay template, I am not understanding what to select for "Post Selection" when adding a field.
[wpv-conditional if="( NOT(empty($(wpcf-riposte-to))) )"]
<div class="riposte-link">A riPOSTe to: ????? </div>
[/wpv-conditional]

FYI: I would like to keep the older Post Reference custom field "riPOSTe" so I know which essays need it and then I will delete.

#909131

Another thing is that the current custom field - "select essasy" - has a default set to the first essay on the list. I need the default to be no essay as only a few essays will get an active field. In other words, this is a rare condition for an essay and that is why I have a conditional (see above) to only display if the field is filled.

Thanks,
Will

#909410

Minesh
Supporter

Languages: English (English )

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

1) I tried to change the naming of the custom field to "riPOSTe to" instead of "select essasy" and failed to replicate the drop down menu of essays. Of course, I made sure to replace the "case" in the hook to reflect that name change, but I guess I missed some other change in the function. If I want to rename the custom field, what should I change?
FYI: I would like to keep the older Post Reference custom field "riPOSTe" so I know which essays need it and then I will delete.
==> Well - as you want to have current connection - its not wise to rename the field as once you rename the field your connections will be lost. I suggest to create a brand new field as per your need and give it your desired name and then once you migrate all connections you should delete the "riPOSTe to" post reference field.

2) In my attempt to insert this custom field value into an essay template, I am not understanding what to select for "Post Selection" when adding a field.
[wpv-conditional if="( NOT(empty($(wpcf-riposte-to))) )"]
<div class="riposte-link">A riPOSTe to: ????? </div>
[/wpv-conditional]
==> We will deal each question one by one. Please open a new ticket for your each new question. This will help other users searching on the forum.

Another thing is that the current custom field - "select essasy" - has a default set to the first essay on the list. I need the default to be no essay as only a few essays will get an active field. In other words, this is a rare condition for an essay and that is why I have a conditional (see above) to only display if the field is filled.
==> Ok - I've changed the code as given under:

add_filter( 'wpt_field_options', 'func_dynamic_populate_essey', 10, 3);
function func_dynamic_populate_essey( $options, $title, $type ){
    switch( $title ){
        case 'select essasy':
            $options = array();
            $args = array(
                'post_type' => 'essay',
				'numberposts' => -1,
                'post_status' => 'publish');

          
            $posts_array = get_posts( $args );

            // adding default option  
			 $options[] = array(
                    '#value' => 0,
                    '#title' => "Please Select",
                );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

You can change "Please Select" option title if required as per your need.

#909811

Minesh
Supporter

Languages: English (English )

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

Could you please mark this resolved and kindly open a new ticket with your each new questions you may have. I would be happy to help. Thank you.

#910759

Thank you

#910762

sorry, i placed my message in the wrong place.

#911048

Minesh
Supporter

Languages: English (English )

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

Ok - you may close this now and we will continue with another ticket.

#911250

Thanks