问题描述:

What is the efficient way of getting meta values of a custom post type in wordpress?

So far there are two ways I know.

  1. First is to create a custom query like:

    SELECT ... (SELECT meta_value FROM wp_postmeta WHERE post_id = wp.ID and meta_key

    = 'fname') as fname .. FROM wp_posts wp INNER JOIN wp_postmeta pm ON pm.post_id

    = wp.id WHERE post_type = 'customer'

  2. Use get_post_meta.

    $meta_values = get_post_meta($post_id)

    then use a foreach loop

网友答案:

All WordPress post meta data is internally cached, running your own SQL query will not have this benefit (unless you implement your own caching).

The function get_post_meta() calls get_metadata(), which in turn calls wp_cache_get(). And from that point on, where it goes inside the class WP_Object_Cache, I got lost...

Anyway, I've read from multiple sources (WordPress Development, WP Forums, wp-hackers mailing list) that using get_post_meta is the way to go.

相关阅读:
Top