Snippets Collections
// add this to functions.php
//register acf fields to Wordpress API
//https://support.advancedcustomfields.com/forums/topic/json-rest-api-and-acf/

function acf_to_rest_api($response, $post, $request) {
    if (!function_exists('get_fields')) return $response;

    if (isset($post)) {
        $acf = get_fields($post->id);
        $response->data['acf'] = $acf;
    }
    return $response;
}
add_filter('rest_prepare_post', 'acf_to_rest_api', 10, 3);
<?php

add_filter( 'acf/validate_value/name=property_size', 'validate_property_size_field', 10, 4 );

function validate_property_size_field( $valid, $value, $field, $input ){

  // bail early if value is already invalid
  if ( ! $valid ) { return $valid; }

  $property_type = $_POST['acf']['field_5f0aa92348bb6'];

  if ( $property_type == 'Rent' ){
    if ( ! $value ) {
      $valid = __( 'This field is required for Rental Properties' );
    }
  }

  return $valid;

}
star

Wed Nov 30 2022 17:21:52 GMT+0000 (UTC) https://stackoverflow.com/questions/56473929/how-to-expose-all-the-acf-fields-to-wordpress-rest-api-in-both-pages-and-custom

#php #wp #acf #rest
star

Thu Sep 08 2022 08:24:18 GMT+0000 (UTC) https://itsmereal.com/advanced-custom-fields-conditional-required-field/

#wordpress #acf #require #field #conditionally #filter

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension