Home Forums Backend Issues (wp-admin) Append custom fields to title and store in database


Append custom fields to title and store in database

  • Hi.

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

    Not only because we have a backlog of articles from our previous site that have the complete title in the database, but also because the standard search option of wordpress doesn’t pick up the custom fields. Not to mention the many templates we need to adjust to get the thing displayed correctly everywhere…

    So, so far I came up with this, which is of course a faulty code.
    Loosely based upon an example I found on this forum

    Can anybody help me with this tough nut please ?

    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 post (of 1 total)

The topic ‘Append custom fields to title and store in database’ is closed to new replies.