Support

Account

Home Forums ACF PRO Possible to get fields based on type?

Solved

Possible to get fields based on type?

  • Is there a way that anyone knows of to get all the fields related to a post but only of a certain type? For instance, all the URL fields.

    I think it might be possible using get_fields() and then looping through all the fields and and looking at their type but when I try (code below) it get an error “Catchable fatal error: Object of class WP_Post could not be converted to string in /home/artscr6/public_html/tpsonline/wp-content/plugins/advanced-custom-fields-pro/api/api-value.php on line 42”

    Wondering if this is possible….

    $allFields = get_fields($post_id);
    	foreach ($allFields as $field) {
    		$fieldObj = get_field_object($field);
    		if($fieldObj->type == 'url') {
    			if (get_field($fieldObj->key, $post_id) == 'http://') {
    						update_field($fieldObj->key, '', $post_id);
    					}
    				}
    			}
  • What version of ACF are you using. The error you’re getting was fixed in 5.3.9.1

    you loop should be

    foreach ($allFields as $field => $value) {
    		$fieldObj = get_field_object($field);
    		if($fieldObj['type'] == 'url') {
    
  • Thank you, that helped. I had the loop syntax wrong.

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

You must be logged in to reply to this topic.