Home / tut_cpt / What is Post Meta? An intro to WordPress Custom Fields
  • How to What is Post Meta? An intro to WordPress Custom Fields


    Here on wpsnipp, we post a lot of snippets revolving around post meta. You’ve undoubtably seen snippets with function calls like get_post_meta( $post_id, $meta_key, true ) or update_post_meta( $post_id, $meta_key, $new_value ), but you may not understand what WordPress is actually doing.

    You may have also heard that WordPress has something called "custom fields." The WordPress Codex says this in relation to custom fields, "This arbitrary extra information is known as meta-data." So as you may have suspected, custom fields, post meta, and meta-data in WordPress are all referring to the same thing. This arbitrary extra information is saved to a WordPress post object and is associated with the post ID.

    Post meta works the same for all WordPress post objects. What this means is we can store and use post meta for pages or custom post types in exactly the same way we do for actual posts.

    But what kinds of things can or should we store as post meta? The short answer: Almost anything. The Codex uses these examples:
    • Mood: Happy
    • Currently Reading: Cinderella
    • Listening To: Rock Around the Clock
    • Weather: Hot and humid
    You could also store things like:
    • Post background color: #FF0000
    • Subtitle: My Post Subtitle
    • Show Social Links?: true
    The types of information you can store is nearly unlimited, However if you're planning on storing categorical information, and don't want to use WordPress' built-in Categories or Post Tags, you may want to forgo post meta, and add your own custom taxonomy instead.

    Custom fields have a built-in rudimentary UI* in the post editor. You may need to enable them.

    Custom fields have a built-in rudimentary UI* in the post editor. You may need to enable them.

    If you temporarily drop the following snippet into your theme's functions.php file, you'll be able to see ALL the custom meta (fields) data associated with a given post from within the post editor page. ( click code to copy )

    wordpress snippet : PHP

    add_action( 'all_admin_notices', 'wpsnipp_show_all_custom_fields' );
    function wpsnipp_show_all_custom_fields() {
        if ( isset( $_GET['post'] ) ) {
            $post_id = absint( $_GET['post'] );
            <div id="message" class="updated">
                <h3>All post meta:</h3>
                <xmp><?php print_r( get_post_meta( $post_id ) ); ?></xmp>
    Doing this is a great way to determine if your post meta was properly saved, or if it exists. Let me walk you through the output of this snippet via the screenshot that follows. get_post_custom [google/]
    1. This is the post meta 'key'. This is the identifier for a piece (or pieces, we'll get to that later) of information. From now on, I'll refer to it as the $meta_key. You'll use that $meta_key in the different function calls:
      • get_post_meta( $post_id, $meta_key, true ) In the above snippet, we used get_post_meta with only one parameter passed in, the $post_id, but generally you'll want to pass the $meta_key and $single parameter as well. I'll explain more in #3.
      • update_post_meta( $post_id, $meta_key, $new_value )
      • add_post_meta( $post_id, $meta_key, $meta_value, $unique )
    2. The underscore at the beginning of these two meta keys tell WordPress that they are "hidden" fields, meaning they will not show up in the standard Custom Fields editor. The two you see in this screenshot are meta data fields that WordPress stores for the post revisions feature.
    3. This is the value row of the field you are checking.
    4. Right now you're seeing a value with only one row, but the ability exists to add multiple rows of values via the add_post_meta function. This is not often used, but can be handy for advanced querying of meta data values which is outside the scope of this article. When using the get_post_meta function, you must specify the third parameter to be true in order to get a singular value, which in our case would just be 'sample field value'. If you don't specify, WordPress will default to giving you all the value rows in an 'array' and you'll have to separate the values you need yourself. Again, it is very rare that you would omit (or pass a false value) to that third parameter.
    Ok! We've covered get_post_meta pretty thoroughly in this article. We'll continue on in the next article with an in-depth walkthrough of the update_post_meta and add_post_meta functions. Let us know in the comments if anything is still fuzzy to you!

    *There are many plugins and code libraries that create customized field inputs and custom metaboxes. An example of a plugin is Advanced Custom Fields, and example of a code library is Custom Metaboxes and Fields.

    Part 1 of a 5 part series (1, 2, 3, 4, 5)
    Published Mondays at 9am.

    ( WordPress codex functions, hooks, in this snippet. )

    query, get_post, add_post_meta, get_post_custom, get_post_meta, update_post_meta, add_action, absint, _n, _e, wp,