Relational custom field

This topic contains 1 reply, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 7 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #39253
    hodor3
    hodor3
    Participant

    Hello,
    I have an issue with relational custom fields in search results:
    The FIRST form([wd_asp id=2]) on this page http://13.38.54.37/cerca-reparti-e-servizi/ searchs inside “reparti” post type. The single “Reparti” post has a custom field called “sedi”: it is a relational custom field nested inside the field “informazioni_generali”.
    In the avanced options http://13.38.54.37/wp-admin/admin.php?page=asp_main_settings&asp_sid=2#7 I’ve set “Advanced Title Field” in this way: {titlefield} Sede: {informazioni_generali_sede}, trying to show in the results the title of “sede” but nothing happen. It works with simple fields(for example it works with {informazioni_generali_ubicazione}) but not with relational field.
    To test it visit this page http://13.38.54.37/cerca-reparti-e-servizi/ and type in the first form “Cardiologia”, you will see the results from “reparti” post type and after the label “Sedi:” nothing.
    How can I print the title of a relational custom field?

    Waiting for a feedback
    best regards

    #39263
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Relational and object storage fields will not work this way, as those have to be queried exactly by structure in a loop or a single post environment.

    The only way to print this data is to use custom code, to fetch all the needed information and add it to the content field, fetched on demand exactly as needed. I am not sure about your exact field setup, but via using the asp_results hook, this could be a potential way of getting the field values:

    add_filter('asp_results', 'asp_fetch_acf_relationship', 10, 1);
    function asp_fetch_acf_relationship($results) {
        $relationship_field_name = 'informazioni_generali';
        $field_name = 'sedi';
    
        // ----------------------------------------------------------------
        if ( function_exists('get_field') ) {
    		foreach ( $results as &$r ) {
    		   $items = get_field($relationship_field_name, $r->id);
    		   if ( is_array($items) ) {
    			   foreach ($items as $item) {
    				  if ( isset($item->ID) ) {
    					  $custom_field = get_field( $field_name, $item->ID );
    					  $r->content .= $custom_field;
    				  }
    			   }
    		   }
    		}
        }
    
        return $results;
    }

    But once again this is just a possible sample, it highly depends on the field structure.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


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

You must be logged in to reply to this topic.