0
0
Wordpressframework~5 mins

Why metadata extends content in Wordpress

Choose your learning style9 modes available
Introduction

Metadata adds extra information to content so it can be better organized and understood.

When you want to add author name or publish date to a blog post.
When you need to store tags or categories for a page.
When you want to add custom details like ratings or locations to content.
When you want to improve search and filtering of posts.
When you want to display extra info without changing the main content.
Syntax
Wordpress
add_post_meta($post_id, $meta_key, $meta_value, $unique = false);

get_post_meta($post_id, $meta_key, $single = false);

add_post_meta adds metadata to a post identified by $post_id.

get_post_meta retrieves metadata for a post. Use $single = true to get one value.

Examples
Adds the author name 'Jane Doe' as metadata to the post with ID 123.
Wordpress
add_post_meta(123, 'author_name', 'Jane Doe');
Adds a unique rating value of 5 to the post with ID 123.
Wordpress
add_post_meta(123, 'rating', 5, true);
Retrieves the author name metadata for post 123.
Wordpress
$author = get_post_meta(123, 'author_name', true);
Gets all tags metadata for post 123 as an array.
Wordpress
$all_tags = get_post_meta(123, 'tags', false);
Sample Program

This example adds two pieces of metadata to a post: location and event date. Then it retrieves and prints them.

Wordpress
<?php
// Add metadata to a post
$post_id = 101;
add_post_meta($post_id, 'location', 'New York');
add_post_meta($post_id, 'event_date', '2024-07-01');

// Retrieve and display metadata
$location = get_post_meta($post_id, 'location', true);
$event_date = get_post_meta($post_id, 'event_date', true);

echo "Event Location: $location\n";
echo "Event Date: $event_date\n";
?>
OutputSuccess
Important Notes

Metadata does not change the main content but adds useful extra details.

Use metadata to help organize and filter content easily.

Always use unique keys for different metadata to avoid confusion.

Summary

Metadata stores extra information about content without changing it.

It helps organize, filter, and display content better.

WordPress provides simple functions to add and get metadata.