Skip Navigation

[Résolu] Selector in Connect existing Object (from relationship) does not load titles

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
When trying to connect existing posts the post selector is full of blank entries.

Solution:
The client is creating posts without titles. The post selector in the relationships UI uses the post title to choose posts, and so the post title must be present.

This support ticket is created Il y a 4 années et 10 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 6 réponses, has 2 voix.

Last updated by Jevgenijs Il y a 4 années et 10 mois.

Assisted by: Nigel.

Auteur
Publications
#1260901

I am trying to: Connect Archival Object to Person from relationship

Link to a page where the issue can be seen: hidden link (example with Elisabeth Hill)

I expected to see: after pressing Connect existing Archival Object, Titles of objects should load.

Instead, I got: Instead empty or "Auto Draft" entities appear in select UI.

The reverse attempt of attaching Person to Object is also impossible because of the same issue.

#1260905
Screenshot 2019-06-06 at 11.16.53.png
Screenshot 2019-06-06 at 11.25.42.png

I attached screenshots illustrating the problem

#1261101

I believe this issue arises when CPT in relationship does not have wp-post-title. This is a problem, because customer has a set of custom fields with Title of object is defined as a custom text field.

Question: how can I get that custom Titles from that fields in select UI for "Connect existing [something]"?

#1261149

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

The selector is based upon the post title, so if your posts do not have titles then, yes, that will not work.

How are the posts—with no title—being created?

If they are being published from the front-end it would be easy enough to automatically update the title based upon a custom field value using the Forms API (e.g. the cred_save_data hook: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data).

If the posts are being submitted from the backend you could do something similar with WordPress's own API, e.g. the save_post hook (https://developer.wordpress.org/reference/hooks/save_post/).

#1261853

No, posts are created from admin part. Are "post titles" obligatory and not replaceable? If yes, it seems I have to find way to auto-generate them. Is there a way to auto-generate post titles on existing posts from custom field?

#1261869

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Post titles are only obligatory inasmuch as that's what is used in the post selector for identifying posts. It's pretty unusual for posts to not have a title at all.

So, yes, you'll want to auto-generate titles based on the custom field.

If you have existing data to update, you can add PHP at Toolset > Settings > Custom Code, which helpfully has the option to run code one time only.

So you can run a batch job that gets the posts that need updating (get_posts), iterates over them and gets the custom field value (get_post_meta) and then updates the post (wp_update_post).

#1261909
Screenshot 2019-06-07 at 10.42.20.png

My issue is resolved now. Thank you!
I found answer in here - https://toolset.com/forums/topic/add-auto-generate-post-title-from-fields/#post-1213248
So I adopted this approach and used WP Bulk Edit to mass update Auto Draft and empty titles after placing this code to functions.php

function sha_autogenerate_title2( $post_id, $post ){
       
  if ( 'archival-objects' == $post->post_type ) {
         
    $archobj_titel = get_post_meta( $post_id, 'wpcf-title-of-archival-object', true );
         
    $new_title = $archobj_titel;
    $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', 'sha_autogenerate_title2',30,2);
 
        // update the post, which calls save_post again
        wp_update_post( $args );
 
        // re-hook this function
        add_action('save_post', 'sha_autogenerate_title2',30,2);
 
     
  }  
}
add_action( 'save_post', 'sha_autogenerate_title2', 30, 2 );
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.