跳转到主要内容

acf/fields/relationship/result

Description

Filters the text displayed for each post in the Relationship field.

Changelog

  • Added in version 4.1.2

Parameters

apply_filters( 'acf/fields/relationship/result', $text, $post, $field, $post_id );
  • $text (string) The text displayed for this post (the post title).
  • $post (WP_Post) The Relationship.
  • $field (array) The field array containing all settings.
  • $post_id (int|string) The current post ID being edited.

Modifers

This filter provides modifiers to target specific fields. The following filter names are available: – acf/fields/relationship/result Applies to all fields. – acf/fields/relationship/result/name={$name} Applies to all fields of a specific name. – acf/fields/relationship/result/key={$key} Applies to all fields of a specific key.

Examples

This example demonstrates how to append a custom field value from each post to the text displayed.

functions.php

<?php
add_filter('acf/fields/relationship/result', 'my_acf_fields_relationship_result', 10, 4);
function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {
    $page_views = get_field( 'page_views', $post->ID );
    if( $page_views ) {
        $text .= ' ' . sprintf( '(%s views)', $page_views );
    }
    return $text;
}

This example demonstrates how to display the Order ID for WooCommerce shop orders in a relationship field.

functions.php

<?php
add_filter('acf/fields/relationship/result', 'my_acf_fields_relationship_result_woocommerce_orders', 10, 4);
function my_acf_fields_relationship_result_woocommerce_orders( $text, $post, $field, $post_id ) {
    if ( $post->post_type !== 'shop_order' ) {
        return $text;
    }

    return str_replace( 'Order', 'Order ' . $post->ID, $text );
}