Support

Account

Home Forums Feature Requests Display relationship objects custom fields in relationship search

Solved

Display relationship objects custom fields in relationship search

  • Hi,
    first of all thanks for your indispensable plugin, which make WordPress even greater!

    I’m working for a virtual museum and I need to link artworks to pages.
    I’ve found the best way is to use “relationship” custom field, format “post object” and querying only custom post type.
    It’s simply perfect, with post object you can do everything.
    I’ve enabled the search filter but the elements available are only:
    – featured image
    – post title
    – post type
    What it would be great is to have also a custom field.
    In my case I need to see in the backend, while editing a page with “relationship” field the custom field “code” (like SKU, ISBN…) which really distinguishes one item from the others, since many items / artworks have almost the same name, and even if you can search in “relationship” field, it’s not so easy to find the exact one (some of them couldn’t have the thumbnail).

    Is then possible to implement this feature:
    to be able to choose which custom field to show in “relationship” elements;
    instead of displaying:
    – item name
    – item name
    – item name
    displaying “code” + “title”:
    – C001 item name
    – C002 item name

    I think this would be really useful.

  • Hi @dademaru

    I couldn’t agree more, this would be a great feature, but one which would be hard to add to the core. Perhaps you could use some jQuery to append this ‘SKU’ filter to the relationship field.

    If you view the DOM of the relationship field, you will notice many data- attributes. All of these attributes are posted to the AJAX query which populates the relationship choices (left column).

    If you wrote some jQuery to add in a new text input which on change, would update a data-sku attribute, then this would be posted to the AJAX query.

    Step 2 would be to hook into the relationship query (there is documentation for this on the docs page) and use the posted sku value to modify the WP_Query args!

    Thanks
    E

  • Hi Elliot,

    thanks for your answer.
    I’m not an expert with jQuery, so by now I found this solution:
    In relationship.php I duplicated the featured_image code in the loop and added:

    if( in_array('code', $field['result_elements']) )
    {
    	$code = get_field( 'code' );
    				
    	$title .= '<span class="code">' . $code . '</span> ';
    }

    and then in right list:

    if( in_array('code', $field['result_elements']) )
    {
    	$code = get_field( 'codice', $p->ID );
    					
    	$title .= '<span class="code">' . $code . '</span> ';
    }

    and in the choices array I added:
    'code' => __("Code",'acf'),

    In this way it works perfectly (as shown in the screenshot).
    Is it possible to do the same with a hook or filter in functions.php, so I leave your file unchanged and update friendly?

    Regards,
    Davide

  • Wow,

    thanks for the solution: it’s simply perfect!
    You’re great!

  • I’ll bring back this old topic.
    The solution works indeed, but the custom part is not searchable. If I type any part of the added SKU in the search field, it does not display.
    Any way to fix this?

  • Hi Elliot, as you implement modification on the forms link on the side of the administration?

  • Has anyone been able to do this with a hook as opposed to editing the acf plugin?
    How can I add this code in, and still allow for updates to ACF?

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

The topic ‘Display relationship objects custom fields in relationship search’ is closed to new replies.