Home » Get WP Post Meta Value Of A Given Meta Key

Get the post meta_value of a given meta_key in WordPress. You can also use this to get the value of a custom meta field of a post.

Requirements:

  • WordPress

Syntax.

get_post_meta( $post_id, $key, $single );

Where.

  • $post_id ( int Required ) is the Post ID.
  • $key ( string Optional ) is the meta_key to retrieve, default is .
  • $single ( bool Optional ) is the option whether to return a single value, default is false.

Example.

Get the value of a meta_key total_views_count.

get_post_meta( $post_id, 'total_views_count', true);

The equivalent native SQL Statement would be.

SELECT
    *
FROM
    wp_postmeta
WHERE
    meta_key='total_views_count'
AND
    post_id='$post_id'
;

Notes:

  • Will return an array of values if $single is false.
  • Will return an the value of the meta field if $single is true.

References:


Posted

in

by

Tags:

One response to “Get WP Post Meta Value Of A Given Meta Key”

  1. […] Get WP Post Meta Value Of A Given Meta Key […]

Leave a Reply

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