Get all post meta

/* Get All Post Meta */
function wco_get_all_post_meta($post_id){
global $wpdb;
$results = array();
$wpdb->query("
SELECT `meta_key`, `meta_value`
FROM $wpdb->postmeta
WHERE `post_id` = $post_id
");
foreach($wpdb->last_result as $key => $value){ $results[$value->meta_key] = $value->meta_value; }
return $results;
}

Query by term and custom field

<?php
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->terms.slug = 'tvlt-notes'
AND $wpdb->term_taxonomy.taxonomy = 'tvlt_category'
AND $wpdb->postmeta.meta_key = 'tvlt_sort'
AND $wpdb->postmeta.meta_value <> '9999'
";

$tvlt_posts = $wpdb->get_results($query, OBJECT);
?>

Change meta on all posts

<?php query_posts('showposts=-1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php

$custom_field_name = 'Sort Order';
$desired_field_value = '100';

add_post_meta($post->ID,$custom_field_name,$desired_field_value,true);
the_title();
echo ' - ' . get_post_meta($post->ID,$custom_field_name,true);
echo'<br>';

?>
<?php endwhile; endif; ?>

Default meta - Custom field

<?php

//Set Default Meta Value
function set_default_meta($post_ID){
$current_field_value = get_post_meta($post_ID,'Sort Order',true);
$default_meta = '100';

if ($current_field_value == '' && !wp_is_post_revision($post_ID)){
add_post_meta($post_ID,'Sort Order',$default_meta,true);
}
return $post_ID;
}

add_action('wp_insert_post','set_default_meta');

?>

Shuffle custom fields

<?
$meta = get_post_custom_values("CustomFieldName");
$metas = shuffle($meta);
$count = 0;
foreach ($meta as $m) {
$m = explode(", ", $m);
echo "<div>";
echo "<a href="" . $m[1] . "" target="_blank">";
echo "<img src="/wp-content/uploads/" . $m[2] . "" width="100">";
echo "</a>";
echo "</div>";
$count++;
if ($count == 12) break;
}
?>

Display custom field on page

<!-- Custom Fields AJN -->

<?php

$my_custom_field = get_post_meta($post->ID, 'myCustomFieldName', true);

if ($my_custom_field == '') {

//Empty Field - Do not display
}

else {
echo '<p><strong>Heading for your post: </strong><br />' .
$my_custom_field . '</p>';
}

?><!-- End Custom Fields -->

Get custom field data

<?php

//Get custom field string:

$my_field = get_post_meta($post->ID, 'myCustomFieldName', true); ?>

echo $my_field;

//Get custom field array:

$my_field = get_post_custom_values("myCustomFieldName'");

echo $my_field[0];

?>