Home Forums General Issues Trying to get group field with get_post_meta inside function.php


Trying to get group field with get_post_meta inside function.php

  • Hi,

    I need to make a custom function that add admin column to the admin pannel of custom post type, i have already manage to do that for 2 fields but impossible to get the value of select inside a group with only the group name ?

    `echo get_post_meta( $post_id, ‘type_of_product’, true );’

    inside type of product i got type_of_product_1, type_of_product_2.. etc

  • I searched everywhere and nothing found about how to use get_post_meta for group type fields.
    After I did some tests found the follwing method:

    For example you have a field type group named : group_field_type
    Inside the ‘group_field_type’ you have two text type fields : inside_group_one and inside_group_two

    to get the two text fields inside the group with get_post_meta in php would be:

    $inside_group_one = get_post_meta( get_the_ID(), 'group_field_type_inside_group_one', true );
    $inside_group_two = get_post_meta( get_the_ID(), 'group_field_type_inside_group_two', true );
    echo $inside_group_one.'<br>';
    echo $inside_group_two.'<br>';

    You concatenate the group field name with the field name inside the group.

    Hope it helps.
    Best wishes

  • You can use ACF functions in the filters for admin columns, there is no reason to use get_post_meta().

    But if you want to use get_post_meta() then then meta key you need to use to get a sub field of a group is "{group_field_name}_{$sub_field_name}"

  • There is a big reason to use get_post_meta instead of ACF functions :
    Way less queries to the database and less load on the frontend. Best if you disable acf on the frontend(search for it on google) and use only get_post_meta.
    Tested it with Code Profiler and the difference is huge.

  • Whether or not ACF functions need more queries is more complex then not using get_post_meta().

    For example that function get_field() is only a wrapper for get_post_meta().

    get_post_meta() will cause a query to be done for any meta field that is not already cached.

    Also, whether or not it creates any additional queries depends on if WP has already cached the meta values for the post. WP in many cases does this automatically. If the values are not cached for the post and getting fields is causing additional queries then you can force WP to get all meta values for a post in a single query by calling get_post_meta() without a meta key get_post_meta($post_id). This will not only speed up using ACF functions but it will also increase the performance of get_post_meta() for individual meta keys. I’m going to be honest with you on this though, I am not completely sure when WP does and does not automatically get all post meta values. In short, performance issues are not necessarily attributed to just using ACF functions.

    There are some fields that will decrease performance depending on how they are used. For example image fields, relationship fields, post object fields, and other fields that can return WP objects of any type like posts, terms, etc. These perform extra queries to get these objects. To increase performance it is usually best for these types of fields to get ID values only and then use WP functions to get only what you need.

    Performance issues, as with many things, are a result of how something is used and not that it is at all. Avoiding the use of ACF functions only means that you’re going to need to do all the work yourself if you need formatted values returned for anything that is not a simple text value.

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

You must be logged in to reply to this topic.