• I am trying to achieve the following.

    We have the name of an artist in our title section of wordpress, let’s say ‘artist’ war on drugs.

    With the ACF plugin we have created several custom fields, namely ‘datum’ which holds the day of the concert, and ‘concertlocatie’ which holds the venue location of the gig.

    Not only do we want the titles displayed like so: ‘artist’ :: ‘concertlocatie’, ‘datum’ (something I have been able to do using the get field values), we really need those values to be stored in the database as well.

    So, so far I came up with this, which is of course a faulty code.

    Can anybody help me with this tough nut please ?

    The idea is that when one writes a post, enters a title and fills out the custom fields, that the value entered in the title field, is appended with the custom fields and stored as such in the mysql database.

    Is this at all possible or even wise ?
    When the post is then saved and one returns to the post, it should read
    ‘artist’ :: ‘concertlocatie’, ‘datum’

    Also worth noting is that the ‘concertlocatie’ is a checkbox list, so from time to time, depending on what terms I experimented with, it displays “ARRAY” instead of the actual value.

    //Add and update title
      function my_post_title_updater( $post_id ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;    
        $datum = get_field('datum');
        $concertlocatie = get_field('concertlocatie');
    
        if ( has_category ('camera-eye')) {
        $my_post['post_title'] = $title . get_field('datum',  $datum) . ' (' . get_field('concertlocatie', $concertlocatie) ')';
        }
    
        // Save in the database
        wp_update_post( $my_post );
    
      }
    
      // Run after ACF saves $_POST['fields'] data
      add_action('acf/save_post', 'my_post_title_updater', 20);
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Append custom fields to title and save in database’ is closed to new replies.