Monday, June 27, 2022
HomeWordPress DevelopmentAuto Generate Publish Title from 2 Customized Fields

Auto Generate Publish Title from 2 Customized Fields


I want to generate the publish title from 2 customized fields, ‘job’, which is a taxonomy, plus ‘date’ which is a date picker, not the date of the publish. That is for a customized publish sort referred to as ‘job-updates’.

I might additionally like to cover the title discipline within the editor.

With assist from the ACF group, I’ve the next code which I’ve added to “Code Snippets” nevertheless it would not appear to work. Ideas?

// run operate after acf updates fields
add_action('acf/update_post', 20);
operate title_from_fields($post_id) {
  // examine on your publish sort
  if (get_post_type($post_id) != 'job-updates') {
    return;
  }
  // get the publish
  $publish = get_post($post_id);
  // get the taxonomy discipline
  $phrases = get_field('job', $post_id);
  // taxonomy fields return an array, use the primary time period
  $time period = $phrases[0];
  // get date discipline
  $date = get_field('date', $post_id);
  // set publish title/slug
  $title = $term->title.' '.$date;
  $post->post_title = $title;
  $post->post_name = sanitize_title($title);
  // take away this filter to forestall infinite loop
  remove_filter('acf/update_post', 20);
  // replace the publish
  wp_update_post($publish);
  // re-add this filter
  add_action('acf/update_post', 20);
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments