Skip Navigation

[Resolved] Get custom taxonomy name in CRED auto-generated title

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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 4 replies, has 2 voices.

Last updated by Bruno Simon 7 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#519282

I am trying to: Generate post title from CRED submission using custom taxonomies and custom fields.

I visited this URL: https://toolset.com/documentation/programmer-reference/cred-api/#csd

I expected to see: The taxonomy name

Instead, I got: the taxonomy value + name

Hello, having browsed through all previous posts related to autotitle from CRED submission I ended up with this code, knowing that I want to use several custom taxonomies combined with several custom fields (XXXX to be replaced with my form ID):

//* CRED autotitle
add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
  if ($form_data['id']==XXXX) {
    $auctiontype = get_the_term_list($post_id, 'listing-type', true);    
    $auctiondate = get_post_meta($post_id, 'wpcf-auction-ending', true);
    $auctiondate = date('d-m-Y',$auctiondate);
    $carbrand = get_the_term_list($post_id, 'car-brand', true);
    $title= $auctiontype. ': le ' . $auctiondate. ' - ' . $carbrand;
    $args = array('ID' => $post_id, 'post_title' => $title);
    wp_update_post($args);
  }
}

The resulting title looks like this: 1'En attente de confirmation: le 26-05-2017 – 1Porsche 911' (notice the '1' before each custom taxonomy name), whereas of course I'd like to get this: 'En attente de confirmation: le 26-05-2017 – Porsche 911'
For the record, in order to retrieve the custom taxonomies I also tried the following code, but to no avail (nothing showed up):

$auctiontype = wp_get_post_terms($post_id, 'listing-type');

Any help or hint would be most welcome.
Thanks.

#519325

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I think you are relly near to the solution. Instead of function get_the_term_list(), could you please try following code:

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
  if ($form_data['id']==XXXX) {
    $term_list = wp_get_post_terms($post_id, 'listing-type', array("fields" => "names"));    
    $auctiontype = join("-",$term_list);
    $auctiondate = get_post_meta($post_id, 'wpcf-auction-ending', true);
    $auctiondate = date('d-m-Y',$auctiondate);
   
    $term_list= wp_get_post_terms($post_id, 'car-brand', array("fields" => "names"));
    $carbrand = join("-",$term_list);

    $title= $auctiontype. ': le ' . $auctiondate. ' - ' . $carbrand;
    $args = array('ID' => $post_id, 'post_title' => $title);
    wp_update_post($args);
  }
}

More info:
=> https://codex.wordpress.org/Function_Reference/wp_get_post_terms

#519482

Hello Minesh,
Thanks for the prompt reply.
Indeed, that did the trick; brilliant!
One small additional question though: when using hierarchical taxonomies, it appears that the child is displayed before its parent (if both are selected); F.i. if 850i is a child of BMW, then I get '850i-BMW' in the title. Any way to invert this?
Thanks again.
Bruno

#519488

Minesh
Supporter

Languages: English (English )

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

This is great news that the solution works perfectly for you.

To get taxonomy terms sort in a hierarchical manner you need to add orderby clause. Could you please try following code:


add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
  if ($form_data['id']==XXXX) {
    $term_list = wp_get_post_terms($post_id, 'listing-type', array("fields" => "names","orderby"=>"term_id"));    
    $auctiontype = join("-",$term_list);
    $auctiondate = get_post_meta($post_id, 'wpcf-auction-ending', true);
    $auctiondate = date('d-m-Y',$auctiondate);
    
    $term_list= wp_get_post_terms($post_id, 'car-brand', array("fields" => "names","orderby"=>"term_id"));
    $carbrand = join("-",$term_list);
 
    $title= $auctiontype. ': le ' . $auctiondate. ' - ' . $carbrand;
    $args = array('ID' => $post_id, 'post_title' => $title);
    wp_update_post($args);
  }
}

I hope now you have exact solution as per your requirement.

#519508

Perfectly!
Many thanks.
Cheers, Bruno

The forum ‘Types Community Support’ is closed to new topics and replies.