Custom Fields
Support General Feature Suggestion: one query to get all custom fields in a group

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 3 months, 1 week ago.

  • Creator
  • #15363


    With wp_instert_post we can create and add meta data to the post at the same time.
    Is there a get_post (or even better get_posts) counterpart that will allow us to grab posts with meta fields without the need of using get_post_meta for each custom field?
    Given how metabox defines its custom fields, it should be possible to create a function that will get all fields at once by referencing the field group. I am currently doing something like this but I end up calling the metabox get meta wrapper function in a foreach cycle.
    It would be ideal to make a join and grab a post with all its meta, querying the DB only once.
    Has this been considered?
    Is this already available?

Viewing 1 replies (of 1 total)
  • Author
  • #15376

    Anh Tran

    Hi Mauro,

    If performance is your concern, then using Meta Box helper functions or get_post_meta is totally fine. WordPress will query all post meta only once, cache them and don’t make extra queries if you call get_post_meta multiple times.

    In case you want a single function that returns all meta value, WordPress has get_post_custom() function:

    Just a note that it returns the raw values stored in the post meta, the same as get_post_meta.

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.