Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i get the custom fields of a relationship object? #9

Closed
Pxlat opened this issue Jan 26, 2016 · 17 comments
Closed

How can i get the custom fields of a relationship object? #9

Pxlat opened this issue Jan 26, 2016 · 17 comments

Comments

@Pxlat
Copy link

Pxlat commented Jan 26, 2016

I have a custom post type (Orders) which has custom fields and another custom post type called (address) which also has custom fields, when you add a new order it will ask you to choose an address (post object), When i call the posts endpoint of the type (Orders), it gets only the core fields of (address) post object but i cannot see the (address) custom fields

@airesvsg
Copy link
Owner

Hi @Pxlat,

you should use the filter "acf/rest/{type}/get_fields" to append this data.

add_filter( 'acf/rest_api/orders/get_fields', function( $data ) {
    if ( $data ) {
        $data->address['acf'] = get_fields( $data->id );
    }
    return $data;
}, 10, 1 );

I used the variable $data as object in my example, but can be array.

Can you post the structure of your response?

@Pxlat
Copy link
Author

Pxlat commented Jan 26, 2016

@airesvsg
Copy link
Owner

Try it:

add_filter( 'acf/rest_api/wimo-order/get_fields', function( $data, $request, $response ) {
    if ( $response instanceof WP_REST_Response ) {
        $data = $response->get_data();
    }

    if( isset( $data['acf'] ) ) {
        $data['acf']['shipping_address']->acf = get_fields( $data['acf']['shipping_address']->ID );
    }   

    return $data;
}, 10, 3 );

@Pxlat
Copy link
Author

Pxlat commented Jan 26, 2016

It worked a like charm

@airesvsg
Copy link
Owner

:shipit:

@nsa-yoda
Copy link

This helped fix an issue of mine. Should post that code snippet in the documentation.

@airesvsg
Copy link
Owner

Hi @jsanc623,
yes, I'll do this.
Thanks

@misfist
Copy link

misfist commented Nov 22, 2017

Your solution works in v2, but causes the following fatal error in v3 (the endpoint for this post type is a white screen :().

Uncaught ArgumentCountError: Too few arguments to function Jacobin_Rest_API_Fields::filter_issue_acf_response(), 2 passed in /srv/www/editor-trellis.jacobinmag.com/current/web/wp/wp-includes/class-wp-hook.php and exactly 3 expected :281

My code:

      /**
       * Filter ACF_To_REST_API Response
       *
       * @since 0.3.8
       */
      add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response' ), 10, 3 );
/**
     * Add Meta to Issue Response
     * Modify response from ACF_To_REST_API
     *
     * @link https://github.com/airesvsg/acf-to-rest-api/issues/9
     *
     * @since 0.3.8
     *
     * @param obj $data
     * @param obj $request
     * @param obj $response
     * @return obj $data
     */
    public function filter_issue_acf_response( $data, $request, $response ) {
      if ( $response instanceof WP_REST_Response ) {
          $data = $response->get_data();
      }

      if( isset( $data['acf']['article_issue_relationship'][0] ) ) {
        $issue_id = $data['acf']['article_issue_relationship'][0]->ID;
        if( $issue_id ) {
          $data['acf']['article_issue_relationship'][0]->issue_number = (int) get_post_meta( $issue_id, 'issue_number', true );
          $data['acf']['article_issue_relationship'][0]->issue_season = get_post_meta( $issue_id, 'issue_season', true );
        }
      }

      return $data;
    }

Any idea why this is and who it can be fixed?

@airesvsg
Copy link
Owner

airesvsg commented Nov 22, 2017

Hi @misfist,

Read again:

Uncaught ArgumentCountError: Too few arguments to function Jacobin_Rest_API_Fields::filter_issue_acf_response(), 2 passed in /srv/www/editor-trellis.jacobinmag.com/current/web/wp/wp-includes/class-wp-hook.php and exactly 3 expected :281

Now, replace 3 to 2 in below code 😃

add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response' ), 10, 3 );

In V3 the $response parameter not exists.

public function filter_issue_acf_response( $data, $request ) {
	// your code here
}

https://github.com/airesvsg/acf-to-rest-api#filters

Thanks

@misfist
Copy link

misfist commented Nov 22, 2017

Thanks @airesvsg.

Changing the number of expected arguments alone doesn't solve the problem.

The issue is that the 3rd argument $response, which is needed by the function is now provided as the 2nd argument. So, I needed to change the function to:

public function filter_issue_acf_response( $data, $response ) {
   if ( $response instanceof WP_REST_Response ) {
          $data = $response->get_data();
      }

      if( isset( $data['acf']['article_issue_relationship'][0] ) ) {
        $issue_id = $data['acf']['article_issue_relationship'][0]->ID;
        if( $issue_id ) {
          $data['acf']['article_issue_relationship'][0]->issue_number = (int) get_post_meta( $issue_id, 'issue_number', true );
          $data['acf']['article_issue_relationship'][0]->issue_season = get_post_meta( $issue_id, 'issue_season', true );
        }
      }

      return $data;
}

Also, these changes breaks version 2. So, I added a condition to get the version.

@airesvsg
Copy link
Owner

V2

add_filter( 'acf/rest_api/{type}/get_fields', function( $data, $response, $request ) {
}, 10, 3 );

V3

add_filter( 'acf/rest_api/{type}/get_fields', function( $data, $response ) {
}, 10, 2 );

@misfist
Copy link

misfist commented Nov 22, 2017

@airesvsg - Yes, I have it working now.

I added this:

      $this->version = get_option( 'acf_to_rest_api_request_version' );

      /**
       * Filter ACF_To_REST_API Response
       *
       * @since 0.3.8
       * @since 0.4.7
       */
      if( '2' == $this->version  ) {
        add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response_v2' ), 10, 3 );
      } else {
        add_filter( 'acf/rest_api/issue/get_fields', array( $this, 'filter_issue_acf_response' ), 10, 2 );
      }

@zacksmash
Copy link

zacksmash commented Mar 10, 2018

Is there a way to specify a page slug, in this filter?

Example:

add_filter( 'acf/rest_api/page/get_fields?slug=home', function( $data, $response ) {
  // Do stuff...
}, 10, 2 );

@airesvsg
Copy link
Owner

Hi @zacksmash,

You can use those ways to specify whats do you want filter.

Page url: http://localhost/my-slug

add_filter( 'acf/rest_api/page/get_fields', function( $data, $response ) {
	$id = $response->get_param( 'id' );

	if ( 'my-slug' === get_post_field( 'post_name', $id ) ) {
		// Do stuff
	}

	return $data;
}, 10, 2 );

OR

Endpoint: http://localhost/wp-json/acf/v3/pages/2

add_filter( 'acf/rest_api/page/get_fields', function( $data, $response ) {
	$id = $response->get_param( 'id' );

	if ( 2 === absint( $id ) ) {
		// Do stuff
	}

	return $data;
}, 10, 2 );

Thanks

@zacksmash
Copy link

@airesvsg Thanks for the reply!

That's what I am currently doing, I just wanted to make sure that it was the recommended way. It's be nice to have a way to specify a specific page by ID or slug, but this works for now!

@airesvsg
Copy link
Owner

https://github.com/airesvsg/acf-to-rest-api#get-acf-fields-recursively

@whynotadv
Copy link

whynotadv commented Sep 19, 2022

I'm not sure if this is the right place to ask for help but I'm struggling with getting the title of a post object field in my relationship filter. You'll see community referenced in my code below. The custom field 'show_as_community_model' is the relationship field that allows you to select a 'home_listing' and 'community' is a post object that resides in the home listing information field group of ACF. So why won't the 'community' post object field title show up in the filter. All the other simple ACF text fields show up perfectly using my code here....

add_filter('acf/fields/relationship/result/name=show_as_community_model', 'my_acf_fields_relationship_result', 10, 4);
function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {

  $community = get_field( 'community', $post->ID ); 
  $sqft = get_field('sq_ft', $post->ID);
  $mls = get_field('mls', $post->ID);
  $price = get_field('price',$post->ID);

    if ($post->post_type == 'home_listings') {
		
        $text .= '  •  Community:' . $community . '  •  Lot:  ' . $lot . '  •  SqFt:  ' . $sqft . ' •  MLS: ' . $mls . ' •  Price:  ' . $price . '';
             }
    return $text;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants