0

Im trying to set a custom post title with a custom field instead of typing the title im setting it with a custom field it works good if I use a type text custom field but once I use a relationship field Im getting nothing I guess because that field return a array, my relationship field is "route_affected_by_this_alert"

// Auto fill Title and Slug for CPT's - 'alerts'
function lh_acf_save_post( $post_id ) {

    // Don't do this on the ACF post type
    // if ( get_post_type( $post_id ) == 'project' ) {
    //     return;
    // }

    $new_title = '';
    $new_slug = '';

// Get title from 'alerts' CPT acf field 'route_affected_by_this_alert' if ( get_post_type( $post_id ) == 'alerts' ) { $new_title = get_field( 'route_affected_by_this_alert', $post_id ); $new_slug = sanitize_title( $new_title ); }

    // Prevent iInfinite looping...
    remove_action( 'acf/save_post', 'lh_acf_save_post' );

    // Grab post data
    $post = array(
        'ID'            => $post_id,
        'post_title'    => $new_title,
        'post_name'     => $new_slug,
    );

    // Update the Post
    wp_update_post( $post );

    // Continue save action
    add_action( 'acf/save_post', 'lh_save_post' );

    // Set the return URL in case of 'new' post
    $_POST['return'] = add_query_arg( 'updated', 'true', get_permalink( $post_id ) );
}
add_action( 'acf/save_post', 'lh_acf_save_post', 10, 1 );

I want to assign those names to the post title

Want to get those names in the post title

This is what im getting:


    Array ( [0] => WP_Post Object ( [ID] => 4009 [post_author] => 1 [post_date] => 2020-05-22 11:29:55 [post_date_gmt] => 2020-05-22 15:29:55 [post_content] =>
Route 49 schedule - Effective 1/17/15
Kennedy Plaza Bus Stop Locations

[post_title] => Camp St/Miriam Hospital [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => 49 [to_ping] => [pinged] => [post_modified] => 2020-05-27 14:44:24 [post_modified_gmt] => 2020-05-27 18:44:24 [post_content_filtered] => [post_parent] => 0 [guid] => https://mypage.com/?post_type=routes&p=4009 [menu_order] => 0 [post_type] => routes [post_mime_type] => [comment_count] => 0 [filter] => raw ) [1] => WP_Post Object ( [ID] => 4000 [post_author] => 1 [post_date] => 2020-05-22 10:59:15 [post_date_gmt] => 2020-05-22 14:59:15 [post_content] =>
Route 60 schedule - Effective 1/19/19

Kennedy Plaza Bus Stop Locations

Newport Visitors Center Bus Stop Locations

All inbound and outbound trips service Bay View Apartments.

[post_title] => Providence/Newport [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => 60 [to_ping] => [pinged] => [post_modified] => 2020-06-08 11:02:14 [post_modified_gmt] => 2020-06-08 15:02:14 [post_content_filtered] => [post_parent] => 0 [guid] => https://mypage.com/?post_type=routes&p=4000 [menu_order] => 0 [post_type] => routes [post_mime_type] => [comment_count] => 0 [filter] => raw ) )


This is what im getting to make it work with the alerts CPT: after: // generate our post title



// Code to get the title from a repeater field

function create_relationship_title($posts)
{

    // set empty net title array
    $new_title = [];

    // if posts is not an array then return false
    if (!is_array($posts)) return false;

    // loop through each post
    foreach ($posts as $post) {

        // add the current post title to new title array
        $new_title[] = $post->post_title;

    }

    // join all the related post titles together separated by a comma
    $new_title = implode(' AND ', $new_title);

    // return the new title
    return $new_title;

}



    function lh_acf_save_post($post_id ) {

    $new_title = '';

    // Get title from 'alerts' CPT acf field 'route_affected_by_this_alert'
    if ( get_post_type( $post_id ) == 'alerts' ) {
        $relationships = get_field( 'route_affected_by_this_alert', $post_id );
        $new_title = create_relationship_title($relationships);
    }

    // get our product object
    $post = get_post($post_id);

    // generate our post title
    if(get_post_type( $post_id ) == 'alerts') {

        // remove the action
        remove_action('acf/save_post', 'lh_acf_save_post', 20);

        // update the post object
        $post->post_title = $new_title;
        $post->post_name = sanitize_title($new_title);

        // update post
        wp_update_post($post);

        // re add the action
        add_action('acf/save_post', 'lh_acf_save_post', 20);

    }

    // finally, return
    return;

}

  
   

// construct your action here
 add_action( 'acf/save_post', 'lh_acf_save_post', 20 );


0

Updated version...

<?php

/**
 * @param $posts array
 * @return false|string
 */
function create_relationship_title($posts)
{

    // set empty net title array
    $new_title = [];

    // if posts is not an array then return false
    if (!is_array($posts)) return false;

    // loop through each post
    foreach ($posts as $post) {

        // add the current post title to new title array
        $new_title[] = $post->post_title;

    }

    // join all the related post titles together separated by a comma
    $new_title = implode(', ', $new_title);

    // return the new title
    return $new_title;

}

/**
 * @param $post_id int
 */
function lh_acf_save_post($post_id ) {

    // check we are on the correct post type
    if(get_post_type($post_id) <> 'alerts') return;

    $new_title = '';

    // Get title from 'companies' CPT acf field 'company_name'
    if ( get_post_type( $post_id ) == 'alerts' ) {
        $relationships = get_field( 'route_affected_by_this_alert', $post_id );
        $new_title = create_relationship_title($relationships);
    }

    // get our product object
    $post = get_post($post_id);

    // generate our post title
    if($post->post_status == 'publish' || $post->post_status == 'draft') {

        // remove the action
        remove_action('acf/save_post', 'lh_acf_save_post', 20);

        // update the post object
        $post->post_title = $new_title;
        $post->post_name = sanitize_title($new_title);

        // update post
        wp_update_post($post);

        // re add the action
        add_action('acf/save_post', 'lh_acf_save_post', 20);

    }

    // finally, return
    return;

}

// construct your action here
add_action( 'acf/save_post', 'lh_acf_save_post', 20 );
| improve this answer | |
  • I just re-read your question and I miss understood about the relationship field, i've not seen any code from your relationship field array so I'm not sure how to answer – joshmoto Jun 10 at 20:20
  • Thank for answering my friend, Basically what I want is to get the title of a post from a relationship field instead of typing it, I have a custom field type "Relationship" its name is "route_affected_by_this_alert" im adding some posts to that field hoping to get the title and assign it to the post that Im creating, if I use a field type text I can get the title but I dont know how to get the title using a Relationship. – kelvin ramirez Jun 11 at 1:48
  • Can you echo '<pre>'.print_r(get_field( 'route_affected_by_this_alert', $post_id )).'</pre>'; exit; and let me know what is returned. Also in the acf relationship field options you can set the field to return the id or the object. Let me know what is returned and i'll update my answer. – joshmoto Jun 12 at 19:33
  • Thanks for answering my friend I set the relationship post to "post object" when I created it, I put the print_r and im getting this (this is a part): Array ( [0] => WP_Post Object ( [ID] => 4009 [post_author] => 1 [post_date] => 2020-05-22 11:29:55 [post_date_gmt] => 2020-05-22 15:29:55 [post_content] => Route 49 schedule - Effective 1/17/15 Kennedy Plaza Bus Stop Locations [post_title] => Camp St/Miriam Hospital [post_excerpt] => (I want to assign the titles to my post) – kelvin ramirez Jun 12 at 21:33
  • 1
    My friend it works perfect, thank you very much I appreciated a lot your help, you´re a genius! Can I reach out to you for future questions? – kelvin ramirez Jun 13 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.