Home Forums General Issues How do you add [ get_comments_number() ] function in a custom field?


How do you add [ get_comments_number() ] function in a custom field?

  • Hi I’m trying to add a custom field with a function that automatically takes “amount of comments in post” from the posts.

    I’ve tried to copy the php code in the custom field I’ve created but it seems it doesn’t register php.

    I’ve also tried to install “insert PHP” plugin and other exec PHP plugins to no avail.

    Any suggestions?

  • You cannot add PHP to custom fields unless you create or add filters to run on them.

    In ACF5 there are additional hooks other than the ones listed on that page, here is the code in ACF where they are called

    $value = apply_filters( "acf/format_value", $value, $post_id, $field );
    $value = apply_filters( "acf/format_value/type={$field['type']}", $value, $post_id, $field );
    $value = apply_filters( "acf/format_value/name={$field['name']}", $value, $post_id, $field );
    $value = apply_filters( "acf/format_value/key={$field['key']}", $value, $post_id, $field );
  • Sorry I’m a newbie to this, what do you apply in the $value, $post_id, $field ?

    Can u give examples?

    and does this code go into the functions.php in my child theme?

  • this is explained on the page I linked to above

    $value = the value which was loaded from the database
    $post_id = the post ID from which the value was loaded
    $field = An array containing all the field settings for the field which was used to upload the attachment

    Let’s say that you wanted to do sortcoded on all acf text fields

    add_flter('acf/format_value/type=text', 'text_field_do_shortcodes', 10, 3);
    function text_field_do_shortcodes($value, $post_id, $field) {
      return do_shortcode($value);
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.