Get custom field value from specific post

I use this code in functions.php file:

function get_custom_field_value($szKey, $bPrint = false) {
global $post;
$szValue = get_post_meta($post->ID, $szKey, true);
if ( $bPrint == false ) return $szValue; else echo $szValue;}

and this one in my HTML to reference it when I need to get a custom field:

Read More
<?php if ( function_exists('get_custom_field_value') ){
    get_custom_field_value('now_location', true);} ?>

But this works only when I use it inside a post because it takes current post’s field value.

How do I get a field value (or several for that matter) from one exact post?
I guess it has something to do with post’s ID but I don’t know what to change/add to the code.

Related posts

Leave a Reply

1 comment

  1. As @janw suggests It is good to pass the post id as an argument in order to get custom fields for a particular post.

    function get_custom_field_value($szKey,$postId, $bPrint = false) {
    $szValue = get_post_meta($postId, $szKey, true);
    if ( $bPrint == false ) return $szValue; else echo $szValue;}