Home / custom field / Get custom fields globally
  • How to Get custom fields globally

    get_custom_fields_globally

    Adding this to the functions.php of your wordpress theme will allow you to get custom field values globally great form themes that use a lot of custom fields.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function get_custom_field($key, $echo = FALSE) {
        global $post;
        $custom_field = get_post_meta($post->ID, $key, true);
        if ($echo == FALSE) return $custom_field;
        echo $custom_field;
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?php
             get_custom_field('custom-field-name', TRUE);
    ?>
    

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

    get_post, get_post_meta,
  • Zach Shallbetter

    Does one have the ability to specify categories, values and keys?

    • This snippet will not let you specify the category of the post you retrieve the custom field from. This snippet will however let you specify the custom field key just change “custom-field-name”

      get_custom_field(‘custom-field-name’, TRUE);