Post Pic

WordPress tip: Get all custom fields from a page or a post

Sometimes you may need to get all custom fields from a specific post or page. Thanks to Paul, who contributed to WpRecipes, for sharing a function that do the job.

Paste the following functions on the functions.pjp files from your theme. The function have lots of comments so it will help you to understand how it works.

function all_my_customs($id = 0){
    //if we want to run this function on a page of our choosing them the next section is skipped.
    //if not it grabs the ID of the current page and uses it from now on.
    if ($id == 0) :
        global $wp_query;
        $content_array = $wp_query->get_queried_object();
        $id = $content_array->ID;
    endif;   

    //knocks the first 3 elements off the array as they are WP entries and i dont want them.
    $first_array = array_slice(get_post_custom_keys($id), 3);

    //first loop puts everything into an array, but its badly composed
    foreach ($first_array as $key => $value) :
           $second_array[$value] =  get_post_meta($id, $value, FALSE);

            //so the second loop puts the data into a associative array
            foreach($second_array as $second_key => $second_value) :
                       $result[$second_key] = $second_value[0];
            endforeach;
     endforeach;

    //and returns the array.
    return $result;
}

Once done, you can use the function like this:

$result = all_my_customs();
echo $result['my_meta_key'];

This recipe has been submitted by Paul, thanks for him! If you have WordPress knowledges, feel free to share it with us!

18 Responses

Oct 02 2009 15:37

What´s the advantage of this over get_post_meta() or get_post_custom()?

Oct 02 2009 16:00

Thanks for commenting in the code..really helps understanding the code..makes it much simple.

Oct 02 2009 17:25

Ah nice, thanks for this tipp!

Oct 03 2009 20:41

yea, second function published on the site…

the reason you would use this is that it grabs all the meta keys and values and puts them into an array, so its so much neater than having 30 variables then having to assign them individually, the way this works is that they are all in they array, so you just call $array['my_meta_key']. and because it does it outside the loop, you can use this to change your loop based on what meta values are present.

I use this to allow users without much knowledge to display all posts in a category and then adjust settings for everything from excerpt length, image width, posts per page etc. to do that with variables is annoying, this way just access the array, and all done from the wp edit page with custom fields. sweet.

Oct 04 2009 05:10

Thx for the recipe, i think i can use it for my post :)

Oct 06 2009 00:44

Nice function. My custom selectbox will be easier then never. \o/

Oct 06 2009 11:58

there is one mistake that is the fault of the html being escaped. the ‘->’ should be ‘->’

Oct 06 2009 11:59

ok, so that didnt work, the -&gt ; should be ->

Nov 02 2009 19:05

Hi, just like Manne I can’t see why the code you provide is better than the very simple calls to the WordPress API such as get_post_custom (to get all postmeta data) or get_post_meta (to get a specific value)

Your last example would become “$result = get_post_custom();” and/or “echo get_post_meta($post->ID, ‘my_meta_key’);”

Am I overseeing anything here?

Nov 09 2009 17:45

qwindoo: i use alot of custom fields is meta boxes on the post and page write sections of the admin page, so getting them all individually and assigning them to a variable is a pain and a little long winded. this way i just assign the variable to the result of the function and i have an array of custom field values for that page. This is both easy to keep track of, because they are in the same array when you want to access them and also makes your code neater when you come back to it months on…

Dec 29 2009 18:02

Hey! Great idea for easier use of custom posts! I am having a problem tho: I keep getting this error.
Warning: array_slice() [function.array-slice]: The first argument should be an array in…wp-content/themes/custom/functions.php on line 29

Which is this line:
$first_array = array_slice(get_post_custom_keys($id), 3);

Any help is appreciated.

Jan 14 2010 12:20

Hi!

I love the wprecipies, but this snippet is not works properly i think. My function better for get all customs (or all values by one key) imho.

Try:

function all_customs($key = ”){
global $wpdb;
$where_key = “WHERE meta_key=’”.$key.”‘”;
$meta = $wpdb->get_results(“SELECT * FROM ” . $wpdb->postmeta . ” ” . $where_key , ARRAY_A);
return $meta;
}

On frontend get with it: $customs = all_customs(‘mykey’); // return with an assoc array

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required