How to get custom field value in wordpress ?

How to get custom field value in wordpress ?

Why We use Custom Field ?

We use Custom field in wordpress for some customization in wordpress which is not by default as a feature.
Lets takes an example – Suppose you need to show Address, City , Name , Designation Or You can set shortcode in custom Field For Specific post too. or you can set other information Related with post.

How to use custom field in wordpress without any plugin ?

You just need to follow some given below steps and you will get the output Definitely.

  • Step 1 : Click on the screen option at the top of right header as you can see in the given picture after that Tick the Custom Field Option.
  • Step 2 : After Tick the Custom Field you will see Custom Field Meta box at the bottom of post where you can create your custom Fields .

Default WordPress Function to Retrieve Custom Field in wordpress ?

//Default value: false

get_post_meta( int $post_id, string $key = '', bool $single = false );

//Retrieves a post meta field for the given post ID.

//get_post_meta Function Parameter Description

$post_id
//(int) (Required) Post ID.

$key
//(string) (Optional) The meta key to retrieve. By default, returns data for all keys.

Default value: ''

$single
//(bool) (Optional) If true, returns only the first value for the specified meta key. This parameter has no effect if $key is not specified.


Lets take another Example

<?php 
 
$MyCustomField= get_post_meta($post->ID, 'my-custom-field', true);
 
if ($MyCustomField) { ?>
 
<p>Today's Show My Custom Field: <? echo $MyCustomField; ?></p>
 
<?php 
 
} else { 
// do nothing; 
}
 
?>

From this process you can easily find the custom field values for post id and use them according to their need

Please Contact us if you have any query related with web development.

Leave a Reply

Your email address will not be published. Required fields are marked *