Skip Navigation

[Resolved] Custom type custom permalinks doesn’t work as expected

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that creates new posts. I would like to automatically set the post title and slug to be the post ID, and I would like to use the permalink format https://mysite.com/post-type-slug/{post-id}

Solution: Use the API hook cred_save_data to automatically set the post title and slug to be the post ID, and choose any pretty permalink format in wp-admin > Settings > Permalinks. Here is a custom code snippet to set the title and slug:

add_action('cred_save_data','tssupp_set_post_id_as_title_and_slug',10,2);
function tssupp_set_post_id_as_title_and_slug($post_id,$form_data) {
  if ($form_data['id']==1234) {
    $args = array('ID' => $post_id, 'post_title' => $post_id, 'post_name' => $post_id);
    wp_update_post($args);
  }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/wp_update_post/
https://wordpress.org/support/article/using-permalinks/

This support ticket is created 4 years, 4 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.

Our next available supporter will start replying to tickets in about 1.36 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by WeiS2074 4 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1704041

Hi ,

continue to the earlier ticket I raised https://toolset.com/forums/topic/custom-type-custom-permalinks-doesnt-work-as-expected/?bbp_reply_to=1682963&_wpnonce=b5d5ed1f05#new-post
It still not working. I installed "Custom Post Type Permalinks" and "Simple Post Type Permalinks", it still doesn't change the permalinks. Now the permalink always starts with "CRED Auto Draft" + random character. How can I change to just simple post id?

#1704385

Hello, it looks like you have a Form that creates custom posts. You want to set the post title, post slug, and post permalink format automatically to be the post ID. I can help you set that up. You can use this custom code snippet:

add_action('cred_save_data','tssupp_set_post_id_as_title_and_slug',10,2);
function tssupp_set_post_id_as_title_and_slug($post_id,$form_data) {
  if ($form_data['id']==1234) {
    $args = array('ID' => $post_id, 'post_title' => $post_id, 'post_name' => $post_id);
    wp_update_post($args);
  }
}

Replace 1234 with the numeric ID of this Form. That is the only change you should make in the code. Then save the code in your child theme's functions.php file, or create a new custom code snippet in Toolset > Settings > Custom Code.

This will set the post title and post slug to be the numeric ID of the post. The default permalink format will be https://yoursite.com/post-type-slug/{post-id}/

Let me know if I misunderstood what you want to accomplish.

#1704703

Thanks a lot. I spent more than 2 days to figure out this problem, I tried many methods and it still not working.
The permalink is almost perfect, it just has a litter problem. the link format is hidden link
Why does it has question mark and equal mark? And my-type comes with the id, not seperated by /

#1705293

Okay so the title and slug are saved correctly, but the permalink structure is defaulting to a query string instead of pretty permalinks. It sounds like a problem in the permalinks formatting, or maybe pretty permalinks are turned off? I'm not sure. I would try to resave permalinks in wp-admin > Settings > Permalinks. Then I would go to Toolset > Post Types and edit this post type. In Options, check Rewrite, it should be turned ON. Please take several screenshots of the Options panel so I can see the other options. I will review your settings and give you some feedback.

#1706659
Capture3.JPG
Capture2.JPG
Capture.JPG

I did try resave wp-admin > Settings > Permalinks to plain, and tick rewrite. it still the same.

#1707839

Okay yes, if you set the Permalinks structure to "Plain", then the URLs will use the "ugly" permalinks format https://yoursite.com/?my-type=12345 instead of "pretty" permalinks. If you want to use the pretty permalink structure https://yoursite.com/my-type/post-slug, you must choose one of the other options in Permalink settings like Day and Name, Month and Name, or Post Name. Sorry for the confusion, I assumed you were aware of the difference between "Ugly" and "Pretty" permalinks but I should have been more clear about the difference: https://wordpress.org/support/article/using-permalinks/

#1708239

I understand I choose the plain structure. But there isn't a structure of hidden link, even in custom structure I don't see this option.

#1708971

But there isn't a structure of hidden link, even in custom structure I don't see this option.
Yes that is correct. The post ID is not offered in any of the options provided by WordPress. So in conclusion:
- Choose one of the "pretty" permalink options. The option you choose will be used to apply "pretty" permalinks to the standard WordPress Posts. It will not change the structure of permalinks for custom post types, but it will activate pretty permalinks.
- Use the custom code we have already discussed to apply the post ID to the post title and post slug of your custom post type
- The custom post type permalink structure will now show up in the URL as yoursite.com/my-type/post-id

#1709359

My issue is resolved now. You are better than the supporter of my last ticket. you have clearly explained the logic behind. Thank you!

Here is the summary for myself.
1. click any option other than Plain in Settings -> Permalinks. I choose Custom Structure, enter /%post_id%/ in the followed textbox.
2. copy your code, change the form id. Form id can be found in Toolset -> post forms
3. in Toolset -> post types -> edit, tick Rewrite, choose "Use the normal WordPress URL logic".
4. if you want to have a custom URL, it can tick "Use a custom URL format". you can use any tag format in Settings -> Permalinks -> Custom Structure