Skip Navigation

[Resolved] 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 6 months, 1 week 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/

#2794786

Hi Minesh,
I am not using front end forms.

I am hoping to develop a PHP snippet that I can use to do this.

Thanks

#2794839

Minesh
Supporter

Languages: English (English )

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

At what course of action you want to genetate the automatic post title? while add/edit custom post in backend admin?

Can you please explain bit more about your actual requirement like for what post type you want to generate the automatic post title using what relationship, what relationship you have and what post types invovled - then I will be able to guide you in the right direction.

#2794845

Hi Minesh,

I have a many-to-one relationship between post types 'Award Recipients' (many) 'Award' (one).

The slug/name of the post types is 'award-recipient' and 'award'.

I have a taxonomy called 'Academic Years' (academic-year) associated with the Award Recipient post type.

When creating an award-recipient, I would like to generate a title using the 'award' (relationship) and the 'academic-year' (taxonomy).

So, I hide the normal WordPress title field, and create the title from selections related ot the relationship and taxonomy.

I have created PHP snippets to auto-generate post titles, but only using normal text fields. As below. But, I cannot see how to access the relationship field value to insert into the title. What I would like is to use something similar to below, but using the relationship and taxonomy to generate the title.

----

function autogenerate_title( $post_id, $post ){

if ( 'student' == $post->post_type ) {

$fname = get_post_meta( $post_id, 'wpcf-first-name', true );
$lname = get_post_meta( $post_id, 'wpcf-last-name', true );

$new_title = $fname . " " . $lname;
$new_title = sanitize_text_field( $new_title );
$new_slug = sanitize_title( $new_title );

$args = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug
);

// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'autogenerate_title',30,2);

// update the post, which calls save_post again
wp_update_post( $args );

// re-hook this function
add_action('save_post', 'autogenerate_title',30,2);

}
}
add_action( 'save_post', 'autogenerate_title', 30, 2 );

#2795111

Minesh
Supporter

Languages: English (English )

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

You can access parent post using the post-relationship API function toolset_get_related_post(): -
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

For example:

$parent_id = toolset_get_related_post( $post_id, 'relationship-slug' );

Where:
-Replace relationship-slug with the slug of your original relationship.

Now based on the $parent_id you can get the parent post object/info and adjust your code accordingly.