获取关系字段的标题

时间:2020-06-10 作者:kelvin ramirez

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 );


1 个回复
最合适的回答,由SO网友:joshmoto 整理而成

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 );

相关推荐