Skip Navigation

[Resolved] Cannot create new CPT

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

Last updated by williamA-6 2 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2492921

Hi,

I am using the following code to auto-generate post titles based on data entered into custom fields. It is working the way I want it to, but since adding it I cannot create any new CTP's (only eidit existing one). Is there anything you can see that would cause this?

Many thanks in advance.

<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

function func_generate_custom_title( $post_id ) {

$post_type = get_post_type( $post_id );

if ( $post_type == 'opportunity' and is_admin()) {

$first_name= get_post_meta($post_id,'wpcf-first-name',true);
$last_name= get_post_meta($post_id,'wpcf-last-name',true);

$custom_title = $first_name." ".$last_name." ";

$updated_data = array(
'ID' => $post_id,
'post_title' => wp_strip_all_tags( $custom_title ),
);
wp_update_post( $updated_data );
} else {
return;
}
}
add_action( 'save_post', 'func_generate_custom_title',30,2);

#2493325

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to create another new post based on the information submitted using the Toolset form.

Lets say for instance, you submit the form and it will create a new post X and you want to create another post Y based on the subtitled information - is that correct?

In addition to that - I would like to have debug information that will help us to debug and investigate your issue:
- https://toolset.com/faq/provide-debug-information-faster-support/

#2493515

Without doing anything!), this seems to have sorted itself out overnight. So whatever the issue was, it is now resolved. Thank you.

#2493667

Correction - this isn't resolved after all!

I have made some minor changes to my original problem and now have CPT called "Buyer" and that has some custom fields ("First Name", "Last Name" and a Post Reference Field called "Registered To").

I am not displaying the post title when creating/editing the CPT "Buyer" as I am using the code below to auto-generate the title when the post is saved. I would like the title to be First name, Last name and the title of the CPT "Registered To"

I now have 3 issue:
1 - The code works for displaying "First Name" and "Last Name", but not "Registered To"
2 - When the code is active, it is impossible to to create a new "Buyer" post (it just hangs). It is possible to edit exising "Buyer" posts, but it just hangs when you click on update.

Is there anything you can see in the code that is (A) causing the performance issue and (B) what do I need to modify in order to display the data in the "Registered To" field in the post title.

I added debugging info in my last update, but let me know if you need access to take a look at things for yourself.

Many thanks in advance.

<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

function func_generate_custom_title( $post_id ) {

$post_type = get_post_type( $post_id );

if ( $post_type == 'buyer' and is_admin()) {

$first_name= get_post_meta($post_id,'wpcf-first-name',true);
$last_name= get_post_meta($post_id,'wpcf-last-name',true);
$registered_to= get_post_meta($post_id,'wpcf-registered-to',true);

$custom_title = $first_name." ".$last_name." ". $registered_to." ";

$updated_data = array(
'ID' => $post_id,
'post_title' => wp_strip_all_tags( $custom_title ),
);
wp_update_post( $updated_data );
} else {
return;
}
}
add_action( 'save_post', 'func_generate_custom_title',30,2);

#2493803

Minesh
Supporter

Languages: English (English )

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

Actually - post reference field is using one-to-many post relationship.

To query the related post reference parent you will have to use the Toolset post relationship API function: toolset_get_related_post

More info:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Could you please try to use the following code and check if that help you to resolve your issue:

function func_generate_custom_title( $post_id ) {

$post_type = get_post_type( $post_id );

if ( $post_type == 'buyer' and is_admin()) {

$first_name= get_post_meta($post_id,'wpcf-first-name',true);
$last_name= get_post_meta($post_id,'wpcf-last-name',true);
$registered_to_id = toolset_get_related_post( $post_id , 'registered-to', 'parent' );

$registered_to = get_the_title($registered_to_id);
 

$custom_title = $first_name." ".$last_name." ". $registered_to." ";

$updated_data = array(
'ID' => $post_id,
'post_title' => wp_strip_all_tags( $custom_title ),
);
wp_update_post( $updated_data );
} else {
return;
}
}
add_action( 'save_post', 'func_generate_custom_title',30,2);
#2493811

Thanks Minesh. There is no longer the "speed issue", but I am just seeing "Auto Draft" for the post title in the Admin View.

#2493815

Minesh
Supporter

Languages: English (English )

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

Can you please try the following code and check if that help you to resolve your issue.

function func_generate_custom_title( $post_id ) {
 
$post_type = get_post_type( $post_id );
 
if ( $post_type == 'buyer' and is_admin()) {
 
$first_name= get_post_meta($post_id,'wpcf-first-name',true);
$last_name= get_post_meta($post_id,'wpcf-last-name',true);
$registered_to_id = toolset_get_related_post( $post_id , 'registered-to', 'parent' );
 
$registered_to = get_post_meta($registered_to_id ,'wpcf-registered-to',true);
  
 
$custom_title = $first_name." ".$last_name." ". $registered_to." ";
 
$updated_data = array(
'ID' => $post_id,
'post_title' => wp_strip_all_tags( $custom_title ),
);
wp_update_post( $updated_data );
} else {
return;
}
}
add_action( 'save_post', 'func_generate_custom_title',30,2);

If above does not help then I'll require admin access details.

*** 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.

#2494321

Minesh
Supporter

Languages: English (English )

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

Here is the final code I've adjusted with the code snippet "auto-generate-post-name":


function func_generate_custom_title1( $post_id ) {
  
$post_type = get_post_type( $post_id );
  
if ( $post_type == 'buyer' and is_admin()) {
  
$first_name= get_post_meta($post_id,'wpcf-first-name',true);
$last_name= get_post_meta($post_id,'wpcf-last-name',true);
  
  
$registered_to_id = toolset_get_related_post( $post_id , 'registered-to', 'parent' );
$registered_to = get_the_title($registered_to_id); 
  
 
$custom_title = $first_name." ".$last_name." ". $registered_to." ";
// unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'func_generate_custom_title1',30,2);
 
  
$updated_data = array(
'ID' => $post_id,
'post_title' => wp_strip_all_tags( $custom_title ),
);
wp_update_post( $updated_data );
        // re-hook this function
        add_action('save_post', 'func_generate_custom_title1',30,2);
  
} else {
return;
}
}
add_action( 'save_post', 'func_generate_custom_title1',30,2);

Can you please confirm it works as expected.

#2494355

Thank you so much Minesh - it works perfectly. My issue is resolved now.