Support

Account

Home Forums Backend Issues (wp-admin) acf/fields/relationship/result only affecting right-hand column

Solved

acf/fields/relationship/result only affecting right-hand column

  • Hello,

    acf/fields/relationship/result is not affecting the left-hand column of the field (the list of available posts, custom posts in my case).

    I’m trying to add a Custom Field value to each post’s title – this is working perfectly but only in the right hand column (i.e. only for posts you’ve selected). I’ve seen other examples of this filter affecting both columns, am I doing anything wrong? ACF PRO version 5.12.2 on WordPress 5.9.3, PHP 7.4.

    In the admin_menu action I’m calling:

    add_filter('acf/fields/relationship/result', [self::class, 'addIdToRelationshipFields'], 10, 4);

    public static function addIdToRelationshipFields($title, $post, $field, $post_id)
        {
            $post_type_singular_slug = get_post_type_object($post->post_type)->labels->singular_name;
            $post_type_singular_slug = strtolower($post_type_singular_slug);
            $post_type_singular_slug = str_replace(' ', '_', $post_type_singular_slug);
            $custom_id = get_field($post_type_singular_slug . '_id', $post->ID);
            if ($custom_id) {
                $title = sanitize_text_field($custom_id) . ' ' . $title;
            }
            return $title;
        }

    Any ideas what I’m doing wrong? Thanks 🙂

  • I just tested this and it is working as expected, altering both sides.

    I did not test this part

    
    $custom_id = get_field($post_type_singular_slug . '_id', $post->ID);
    if ($custom_id) {
      $title = sanitize_text_field($custom_id) . ' ' . $title;
    }
    

    but I don’t see anything wrong with it provide the field you are trying get exists on the post.

  • @hube2 Thanks – I have even tested it with a completely cut-down version and it still only affects the right-hand column:

    public static function addIdToRelationshipFields($title, $post, $field, $post_id)
        {
            return 'WHAT?!';
        }
  • Try increasing the priority of your add_filter to 20

  • @hube2 That didn’t work sadly but THANK YOU nevertheless, because it gave me the idea to run the filter on a different action: I’ve moved it from admin_menu to admin_init and it’s all working now, both columns 🙂

    I suppose I was being dense, in that I was displaying the field on a custom post page, so it wasn’t an admin menu as such… But then I’d have thought that neither column would have worked if that was the actual problem!…

    Anyways, thanks again 🙂

  • acf/fields/relationship/result should not be inside another hook action/filter. The reason that it was not working is that admin_menu is not run on an AJAX request.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.