Skip Navigation

[Waiting for user confirmation] Automatically generate post title based on a relationship

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 2 replies, has 1 voice.

Last updated by Minesh 1 day, 12 hours ago.

Assisted by: Minesh.

Author
Posts
#2794294

Hi,

I want to create post title based on a field from a relationship and a taxonomy.

The components would be the Post Title from the related post.

So I would like the title to be in the form of "Relationship Field - Taxonomy Value" (e.g., Design Reference 2022-23)

I can see how to access normal fields from the post, but I cannot seem to get the relationship field.

Any help would be greatly appreciated.

#2794333

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

You can acess the relationship field value using the types field API:
- https://toolset.com/documentation/customizing-sites-using-php/functions/

For instance - if you have a parent custom field with slug "book-name" - you can get the value of the parent on child form as:

$parent_id = 999;
$parent_book_name = types_render_field( "book-name", array("item"=>$parent_id));

Where:
- Replace 999 with the original parent ID.

To get the parent post term:

      $parent_id = 111;  // adjust your parent post ID here
      $taxonomy_slug = 'book-type';
      $parent_terms = wp_get_object_terms( $parent_id ,$taxonomy_slug); 
  
 
        if(!empty($parent_terms)) {
  
               foreach($parent_terms as $parent_term) {
                 //// do whatever you want
            }
        }

Where:
- Replace 'book-type' with your original taxonomy slug.

More info:
- https://developer.wordpress.org/reference/functions/wp_get_object_terms/

Please let me know if you need further assistance.

#2794411

Hi Minesh,

This is really helpful, and I can see how this can work.

However, this presupposes that there is a known $parent_id, but this would change from post to post. How can I get the $parent_id from the current post? I've been looking at the Programmer Documentation, but I can't seem to find this.

I have two post_types, called Awards ($post_type = 'award') and Award Recipients ($post_type = 'award-recipient'). 'award' has the name of the Award and various general information. The Award Recipient is the person that receives the award, so has name, year of award, etc. There is a (one to many) relationship between Award and Award Recipient.

We don't know the parent_id of the Award, as there may be many awards and many recipients. So, for the code you have shown me, I need a way to get the parent_id from the 'award'.

Is there a way to do this?

Many thanks for your help!

#2794431

Minesh
Supporter

Languages: English (English )

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

I would like to know - at what course of action you want to generate the post title automatically?

Are you using Toolset form? if yes - Can you please tell me on what page/post you added the Toolset post form?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2794562

Minesh
Supporter

Languages: English (English )

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

That is strange. Do you want to conntent the post while adding the post from frontend? If yes:
- Have you checked the Tooslet forms?

More info:
- https://toolset.com/course-lesson/front-end-forms-for-adding-content/
- https://toolset.com/course-lesson/front-end-forms-for-editing-content/