始终返回关系自定义字段

时间:2014-12-12 14:03:31

标签: wordpress advanced-custom-fields

我有一个函数总是返回wordpress中每个帖子的高级自定义字段,但这似乎不适用于按关系链接的帖子对象。知道如何将高级自定义字段自动附加到这些字段吗?

将自定义字段附加到每个帖子对象的函数:

function my_always_get_post_custom( $posts ) {

    for ( $i = 0; $i < count($posts); $i++ ) {

        $custom_fields = get_fields( $posts[$i]->ID );
        $posts[$i]->custom = $custom_fields;

    }

    return $posts;
}
add_filter( 'the_posts', 'my_always_get_post_custom' );

对象显示自定义字段附加到帖子但不附加链接在其中的对象:

WP_Post Object
(
    [ID] => 90
    [post_author] => 1
    [post_date] => 2014-12-12 12:47:58
    [post_date_gmt] => 2014-12-12 12:47:58
    [post_content] => 
    [post_title] => test case
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => test-case
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2014-12-12 13:48:18
    [post_modified_gmt] => 2014-12-12 13:48:18
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://test.dev/?post_type=gk_case&p=90
    [menu_order] => 0
    [post_type] => gk_case
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
    [custom] => Array
        (
            [top_image] => 
            [customer] => Test customer
            [services] => 
            [account_manager] => Array
                (
                    [0] => WP_Post Object
                        (
                            [ID] => 30
                            [post_author] => 1
                            [post_date] => 2014-12-12 12:41:36
                            [post_date_gmt] => 2014-12-12 12:41:36
                            [post_content] => 
                            [post_title] => Jon jonsen
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => jon-jonsen
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2014-12-12 12:41:36
                            [post_modified_gmt] => 2014-12-12 12:41:36
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://test.dev/?post_type=gk_cv&p=30
                            [menu_order] => 0
                            [post_type] => gk_cv
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                        )

                )

            [content] => 
Content test



            [modules] => 
            [excerpt] => Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad dolor dolorem et expedita fuga ipsa itaque maiores optio quasi, quia quisquam, rem repellendus. Ea iusto labore sequi vel! Deleniti, dolores.
        )

)

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取关系的自定义字段。

扩展函数将循环遍历所有自定义字段,并检查是否存在关系字段。如果是,它将循环遍历所有post对象并检索这些对象的自定义字段。

function my_always_get_post_custom( $posts ) {

    // Loop posts
    for ( $i = 0; $i < count($posts); $i++ ) {

        $custom_fields = get_fields( $posts[$i]->ID );

        if( $custom_fields ) {

            $posts[$i]->custom = $custom_fields;

            // Loop custom fields
            foreach( $custom_fields as $field_name => $value ) {

                // get_field_object( $field_name, $post_id, $options )
                // - $value has already been loaded for us, no point to load it again in the get_field_object function
                $field = get_field_object($field_name, false, array('load_value' => false));

                // Check if this is a relationship field
                if ( $field['type'] == 'relationship' ) {

                    // Loop post objects
                    for ( $i = 0; $i < count($value); $i++ ) {

                        // Get custom fields
                        $custom_fields = get_fields( $value[$i]->ID );
                        $value[$i]->custom = $custom_fields;

                    }

                }

            }

        }

    }

    return $posts;

}

add_filter( 'the_posts', 'my_always_get_post_custom' );

这将给出以下输出(示例):

WP_Post Object
(
    [ID] => 90
    [post_author] => 1
    [post_date] => 2014-12-12 12:47:58
    [post_date_gmt] => 2014-12-12 12:47:58
    [post_content] => 
    [post_title] => test case
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => test-case
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2014-12-12 13:48:18
    [post_modified_gmt] => 2014-12-12 13:48:18
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://test.dev/?post_type=gk_case&p=90
    [menu_order] => 0
    [post_type] => gk_case
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
    [custom] => Array
        (
            [top_image] => 
            [customer] => Test customer
            [services] => 
            [account_manager] => Array
                (
                    [0] => WP_Post Object
                        (
                            [ID] => 30
                            [post_author] => 1
                            [post_date] => 2014-12-12 12:41:36
                            [post_date_gmt] => 2014-12-12 12:41:36
                            [post_content] => 
                            [post_title] => Jon jonsen
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => jon-jonsen
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2014-12-12 12:41:36
                            [post_modified_gmt] => 2014-12-12 12:41:36
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://test.dev/?post_type=gk_cv&p=30
                            [menu_order] => 0
                            [post_type] => gk_cv
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                            [custom] => Array
                                (
                                    [custom_field_1] => value
                                    [custom_field_2] => value
                                )
                        )

                )

            [content] => 
Content test



            [modules] => 
            [excerpt] => Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad dolor dolorem et expedita fuga ipsa itaque maiores optio quasi, quia quisquam, rem repellendus. Ea iusto labore sequi vel! Deleniti, dolores.
        )

)

请注意,该功能仅比原始功能更深一级。如果需要,您可以扩展功能。