An elaboration on using Post Meta in WordPress

Thanks to the need for associating more content with a post, we are able to explore the multiple possibilities available with WordPress post meta. If you’re a WordPress user who’s been struggling hard with the incorporation of different types of content in your WP posts, then learning about post meta will definitely help you

See also: Best way to Remove Default WordPress Meta Boxes

What’s been captured in this post?

Here, you’ll get a detailed insight into the significance of using post meta in WordPress along with the right method to proceed ahead with the same.

Why use Post meta for your WordPress website?

Let’s understand the importance of using Post meta in WP with the help of an example. If you’ve got become bored of the plain, similar-looking titles for your WordPress posts and want to equip them with a different text or link or HTML, then using the post meta is a viable option. With post meta, you can conveniently add extra bits to details to an existing post, thereby customize your WP website for a better look and feel.

Adding post meta via WordPress admin panel

To start off, simply login to your WP admin panel. Once you’ve logged in successfully, simply open the Post Edit Page which can be used for adding a post meta to the chosen post. Next, click on “Add New” option available under Posts menu. Here, you’ll be able to view the “custom fields meta box” which contains the post meta. On the contrary, you can click on “Screen Options” link provided towards the top right side of the Post Edit Page and check the “Custom Fields” checkbox for enabling post meta. Here’s a screen-shot for the same:

Post Meta in WordPress

Once you’re done with enabling post meta, the following section will be visible towards the bottom of your Post Edit Page:

WordPress Post Meta easy way

On the above screen, the drop-down list contains all the post meta keys which have already been added to different WP posts. Here, you can choose the meta key which you want to add to the post under focus. However, you are also free to add a new post meta key by clicking on “Enter New” link displayed within the above screen-shot. Once you’re done with specifying a name and value for the post meta, click on “Add Custom Field” for attaching the newly created post meta to the post once the post has been saved or published.

Using Post meta in your WordPress theme

After having added the post meta to your post, use the get_post_meta function for reading the respective post meta. This WordPress function accepts three arguments/parameters listed below:

  • post ID – This argument represents the ID of the post which contains the meta that you intend to read
  • meta key – This argument represents the post meta key that you want to read for the post
  • boolean – Passing ‘true’ as the value for this argument will return meta value in the form of a string and passing ‘false’ as its value will return the meta value in the form of an array.

Apart from the get_post_meta function, WordPress comes loaded with numerous post meta related functions, some of which have been explained below:

  • get_post_custom – this function accepts post ID as the input and returns an array comprising of meta keys and values for the post.
  • get_post_custom_keys – this function takes post ID as the input and returns all the meta keys associated with the post, in the form of an array.
  • get_post_custom_values – This function allows you to fetch all the values for the post meta key associated with the chosen post.
  • delete_post_meta – this function takes the post ID and the related meta key which you want to delete.

Combining Post Meta with Custom Post Type

Collaborating post meta with your custom post type is a one-step process. All you need to do is simply add custom fields into the supports parameter of WordPress built-in function called register_post_type as shown below:

register_post_type( 'Desktops',
array(
'labels' => array(
'name' => __( 'Desktops' ),
'singular_name' => __( 'Desktops' )
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'custom-fields',
)
)
);

That’s it!

Wrapping Up

So, whether you’re looking forward to build a photography site or a corporate portal using WordPress, leveraging the possibilities available with post meta is perhaps something that’s going to work in your favor.

Tags: wordpress post meta, wordpress post meta box, wordpress post meta array, wordpress update post meta, wordpress custom post meta, wordpress post meta description, get_post_meta array, wordpress display custom post meta, wordpress get all post meta, wordpress show meta, wordpress get custom field, get post custom field, wordpress show custom field, wordpress update post meta

About Jack Calder

Jack Calder is working as Web developer in Markupcloud Ltd. Which is the top psd to html conversion company. Instead of all these things he also shares important & useful information regarding new updates.


About Admin

Hi I am Md. Masum Billah Contributor of this website. I always like to share various things with other. In this website I am sharing web design related tips and tricks, necessary web development tools, source codes for beginner and advance web developer. You may join with us to help other who want to learn web design and development. Thank you for stay with.

Leave a Reply

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