Support

Account

Home Forums General Issues Retrieve ID instead of link from 'page_link' field-type

Solved

Retrieve ID instead of link from 'page_link' field-type

  • In the database I can see that the field-type ‘page_link’ saves the ID of a post, while for archives the complete link is being saved.

    Somewhere along the line the saved ID’s are converted to the permalink. For a specific use-case I will need the ID’s of posts, I will handle the archive links myself. How can I disable the conversion of ID to link?

  • You cannot alter the conversion of the links as stored in the database.

    You can get the unformatted value and this will return the ID if that is what is stored.

    $value = get_field('page_link_field', false, false);

    see the documentation for get_field() https://www.advancedcustomfields.com/resources/get_field/

  • Hi John,

    Thanks, this does work.

    However, unfortunately it doesn’t really solve my problem. I will elaborate a bit why.

    We usually build sites where a global variable ($post_sets) is being assigned with all ACF metadata from a post, on single pages and in ‘the loop’. This is done with the function below function.

    function get_post_options($post_id = null) {
    	if (function_exists('get_fields')) :
    		global $post_sets, $post;
    
    		$post_sets = '';
    
    		if (!is_numeric($post_id) && exists($post)):
    			$post_id = $post->ID;
    		endif;
    
    		<strong>$post_sets = get_fields($post_id);</strong>
    
    		// if $post_sets is 'false', we're assigning an empty array to it
    		// that's more handy for error handling.
    		if ($post_sets == false) {
    			$post_sets = array();
    		}
    
    		return $post_sets;
    
    	endif;
    }
    add_action( 'wp', 'get_post_options');
    add_action( 'the_post', 'get_post_options');

    If I would add false to the $post_sets = get_fields($post_id); line, I will lose all (very useful) formatting that’s being done by ACF. I even lose the field names of subfields.

    I really only need to get rid of the formatting that’s being done by the page_link field. Am I wrong that various fields have a way of altering the formatting with a filter? Shouldn’t the page_link field offer such a thing as well?

  • I guess I solved my problem with the following:

    add_action('acf/init', function () {
    	remove_filter('acf/format_value/type=page_link', array(acf_get_field_type('page_link'), 'format_value'), 10);
    });

    Later on I will use get_permalink() on numeric values and leave string values (archive links) untouched.

  • Yes, you can create a filter that alters the formatting. acf/format_value

    Use a priority > 10 so that it runs after ACF has done it’s formatting.

    $value will hold the formatted value
    $field[‘value’] will hold the DB value

  • Hi John,

    I might be understanding you incorrectly. But when I use the code below, the var_dump is returning null.

    function acf_reformat_page_link( $value, $post_id, $field ) {
    		var_dump($field['value']);
    
    		return $value;
    	}
    	add_filter('acf/format_value/type=page_link', 'acf_reformat_page_link', 15, 3);
  • I could be wrong, but in looking I found something else

    
    $check = apply_filters( 'acf/pre_format_value', null, $value, $post_id, $field );
    

    This happens before ACF formats the value. If you return any value other NULL from a filter ACF will return that value and will not do it’s own formatting.

  • Hi John,

    That might work, but I think I will use the solution from this post because in our case we’ll always use the value of the link field combined with a function that will apply it’s own formatting. So it’s OK to just always remove the formatting.

    Though I think that it would be a good thing if ACF would think about introducing an option to enable/disable formatting for this field type, just like various other fields support this.

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

You must be logged in to reply to this topic.