Skip Navigation

[Résolu] Custom name for Many to Many Post Type

This support ticket is created Il y a 7 années et 6 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
- 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 13 réponses, has 3 voix.

Last updated by Cesar Il y a 7 années et 5 mois.

Assisted by: Minesh.

Auteur
Publications
#450189

Hi,
I have a Many to many relationship setup for Artist and Exhibitions. I did create a Custom Post Type to manage many relationship but when my post are generated I got a generic naming "rel-artist-expo-20"

I would like to be able to customize the name used for those posts with one of my post fields or a title from Artist/expo CPT, or a mix of both, so that there is a more visual relationship between them.
Could you please guide me to accomplish it?
Thanks

#450464

Minesh
Supporter

Languages: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

There is no native solution available to change the post title name but you may try to do that using the custom code and use "save_post" action hook in order to update the your CPT's title.

More info:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

Unfortunately, custom programming work is out of the scope of our support polity but If you need custom programming for your site, please don't hesitate to contact our certified partners:
=> https://toolset.com/consultant/

#450655

Hi,
Thanks for your answer I know how to do the WordPress part but i miss how to get data from the toolset.
How can a get the date of the child CPT?
How can I get a specific Field from a group of the child Post Type?

I think this should be a nice feature to have, naming by numbering is not really usefull
Thanks!

function modify_post_title( $data )
{
   if($data['post_type'] == 'rel-artista-expo' && isset($_POST['expo_date'])) {
  	$artistname = get_post_meta($post->ID, 'artistname'); // how can I get a field from a child
    $date = date('l, d.m.Y', strtotime($_POST['expo_date'])); // data is not stored and only get Thursday, 01.01.1970 Unix timestamp first day
    $title = $date . ' Expo ' . $artistname;
    $data['post_title'] =  $title ; //Updates the post title to the new title.
  }
  return $data; // Returns the modified data.
}
#450842

Minesh
Supporter

Languages: Anglais (English )

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

Well - to get values of your child posts using PHP, please check following Doc:
=> https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/
[ Section: "Displaying child posts using Types PHP functions"]

#450912

Great!
But I need the parent, as I said I'm trying to change the title of the CPT for a Many to Many relationship.
Can you point me to the same resource but for a parent? Please 🙂
I did try to find it in the API reference but could'nt
Thanks for your time

#450917

Minesh
Supporter

Languages: Anglais (English )

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

Well - your parent ID is stored with following meta key in postmeta table:

_wpcf_belongs_{parent-psttype-slug}_id

You can retrieve the parent post id using:

$parent_id = get_post_meta($child_post_id, '_wpcf_belongs_{parent-psttype-slug}_id', true);

Where:
Replace {parent-psttype-slug} with your original parent post type slug.

Once you get your parent post ID, get the post based on parent post id and you will get the all info about parent post.

#450930

I can't get parent ID

I'm guessing that $child_post_id should be used in the loop because I get Undefined variable: child_post in /Applications/MAMP/htdocs/...

What is $child_post_id?

Right now my function is re-writing the title and I'm just trying to get the parent ID and add it the title, but I can't because of $child_post_id is Undefined
Does this make any sense?

// Grabs the inserted post data so you can modify it.
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 1 ); 

function modify_post_title( $data )
{
   if($data['post_type'] == 'rel-artista-expo' ) {

	$parent_id = get_post_meta($child_post_id, '_wpcf_belongs_{exposiciones}_id', true);
	    $title = 'Expo_' . $parent_id;
    $data['post_title'] =  $title ; // Updates the post title to the new title.
  }
  return $data; // Returns the modified data.
}

Thanks again!

#450943

Minesh
Supporter

Languages: Anglais (English )

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

Well - { } opening and closing brackets should not there with your parent post key:

_wpcf_belongs_exposiciones_id
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 1 ); 
 function modify_post_title( $data ) {
   if($data['post_type'] == 'rel-artista-expo' ) {
 
    $parent_id = get_post_meta($child_post_id, '_wpcf_belongs_exposiciones_id', true);
        $title = 'Expo_' . $parent_id;
    $data['post_title'] =  $title ; // Updates the post title to the new title.
  }
  return $data; // Returns the modified data.
}

Now, $child_post_id should be assigned with the value your original child post ID.

#451928

Unfortunately, this is not working.
I'm not getting any id and the log error still the same
"
PHP Notice: Undefined variable: child_post_id in /
"

#452274

Hi Cesar,

Minesh is in his holiday, you will get answer tomorrow.

#452531

Minesh
Supporter

Languages: Anglais (English )

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

I would like to know here for which post type you are applying save_post hook? is it for your parent post type or child post type?

As I said the " $child_post_id" variable should assigned with your original child post ID. if you are on child post.

#452598

Yes,
if($data['post_type'] == 'rel-artista-expo' )

rel-asrtista-expo i s a child CPT of Artist and Exhibitions (parent CPT) in order to have a Many to Many relationship

The function is placed in function.php

Thanks

#452613

Minesh
Supporter

Languages: Anglais (English )

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

1)
Could you please share problem URL?

2)
*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#452616

This a production site and I'm working locally

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.