#wcphilly @salcode

Introduction to Post Meta

Sal Ferrarello / @salcode

Fishing Equipment

Twentysixteen Default

Hello World blog post using Twentysixteen default theme

Add Subtitle to Content

Hello World blog post with a heading in the content

template-parts/content-single.php

<header class="entry-header">
	<?php
		the_title(
			'<h1 class="entry-title">',
			'</h1>'
		);
	?>
</header>

template-parts/content-single.php

<header class="entry-header">
	<?php
		the_title(
			'<h1 class="entry-title">',
			'</h1>'
		);
	?>
	<h2>It's a beautiful day</h2>
</header>

Add Subtitle

Hello World blog post with a subtitle under the title

Hardcoded

Introduction to Post Meta blog post with same subtitle because the subtitle is hardcoded
Animated gif showing how to display Custom Fields in the WordPress post editor

Hello World

Post ID: 1

Custom Fields block with name fe_subtitle and value \

template-parts/content-single.php

<header class="entry-header">
	<?php
		the_title(
			'<h1 class="entry-title">',
			'</h1>'
		);
	?>
	<h2>It's a beautiful day</h2>
</header>
<h2>It's a beautiful day</h2>

Subtitle Code

$post_id  = get_the_ID();

$subtitle = get_post_meta( $post_id,
						'fe_subtitle', true );

if ( $subtitle ) {
	echo '<h2>' .  $subtitle . '</h2>';
}

Subtitle Code




						'fe_subtitle'




Custom Fields block with name fe_subtitle and value \
Hello World blog post with dynamic subtitle loaded from Custom Field value

Link to template code

Introduction to Post Meta

Post ID: 4

Custom Field with dropdown with fe_subtitle as selected option and a value of \

Post ID: 4

Introduction to Post Meta blog post with dynamic subtitle loaded from Custom Field value

Post ID: 1

Hello World blog post with dynamic subtitle loaded from Custom Field value

Grocery Carts

Two shopping carts - one large, one small

WordPress is Smarter

Database

A blank spreadsheet

wp_posts

The WordPress wp_posts database table represented as a spreadsheet

wp_postmeta

The WordPress wp_postmeta database table represented as a spreadsheet

get_post_meta()

# In general:
# get_post_meta( $post_id, $meta_key, true );

# Example:
get_post_meta( 4, 'fe_subtitle', true );

wp_postmeta

The WordPress wp_postmeta database table represented as a spreadsheet

Subtitle Code

$post_id  = get_the_ID();

$subtitle = get_post_meta( $post_id,
						'fe_subtitle', true );

if ( $subtitle ) {
	echo '<h2>' . $subtitle . '</h2>';
}

Subtitle Code

$post_id  = get_the_ID();







Subtitle Code

$post_id  = get_the_ID();

$subtitle = get_post_meta( $post_id,
						'fe_subtitle', true );




Subtitle Code

$post_id  = get_the_ID();

$subtitle = get_post_meta( $post_id,
						'fe_subtitle', true );

if ( $subtitle ) {
	echo '<h2>' . $subtitle . '</h2>';
}
Hello World blog post with dynamic subtitle loaded from Custom Field value

Refactored Subtitle Code

$post_id  = get_the_ID();

$subtitle = get_post_meta( $post_id,
							'fe_subtitle', true );

if ( $subtitle ) {
	echo '<h2>' . $subtitle . '</h2>';
}

 

Refactored Subtitle Code

$post_id  = get_the_ID();

$subtitle = get_post_meta( $post_id,
							'fe_subtitle', true );

if ( $subtitle ) {
	echo '<h2>' . esc_html( $subtitle ) . '</h2>';
}

 

Refactored Subtitle Code

$post_id  = get_the_ID();

$subtitle = get_post_meta( $post_id,
							'fe_subtitle', true );

if ( $subtitle ) {
	echo '<h2>' . esc_html( $subtitle ) . '</h2>';
}

Link to template code

Introduction to Post Meta

Post ID: 4

Custom Field with an additional Name/Value pair. Name fe_learn_more, Value https://salferrarello.com/post-meta/

wp_postmeta

WordPress wp_postmeta as a spreadsheet, now including the new fe_learn_more row
get_post_meta( 4, 'fe_learn_more', true );

Display Learn More Link

$post_id = get_the_ID();

$url = get_post_meta( $post_id,
	'fe_learn_more', true );

if ( $url ) {
	echo 'Learn More';
}
Introduction to Post Meta blog post with Learn More button created using the fe_learn_more post meta value

Link to template code

Refactored Learn More Code

$post_id = get_the_ID();

$url = get_post_meta( $post_id,
	'fe_learn_more', true );

if ( $url ) {
	echo '<a href="' .  $url .  '">Learn More</a>';



}

Refactored Learn More Code

$post_id = get_the_ID();

$url = get_post_meta( $post_id,
	'fe_learn_more', true );

if ( $url ) {
	printf(
		'<a href="%s">Learn More</a>',
		$url
	);
}

Refactored Learn More Code

$post_id = get_the_ID();

$url = get_post_meta( $post_id,
	'fe_learn_more', true );

if ( $url ) {
	printf(
		'<a href="%s">Learn More</a>',
		esc_url( $url )
	);
}

Refactored Learn More Code

$post_id = get_the_ID();

$url = get_post_meta( $post_id,
	'fe_learn_more', true );

if ( $url ) {
	printf(
		'<a href="%s">Learn More</a>',
		esc_url( $url )
	);
}

Hello World

Post ID: 1

Custom Field with an additional Name/Value pair. Name fe_color, Value #ff0000

wp_postmeta

WordPress wp_postmeta as a spreadsheet, now including the new fe_color row

Apply Title Color

$post_id = get_the_ID();
$color = get_post_meta( $post_id,
	'fe_color', true );

if ( ! $color ) { $color = 'black'; }

printf(
	'<header style="color: %s;">',
	esc_attr( $color )
);

Apply Title Color

$post_id = get_the_ID();
$color = get_post_meta( $post_id,
	'fe_color', true );







Apply Title Color

$post_id = get_the_ID();
$color = get_post_meta( $post_id,
	'fe_color', true );

if ( ! $color ) { $color = 'black'; }





Apply Title Color

$post_id = get_the_ID();
$color = get_post_meta( $post_id,
	'fe_color', true );

if ( ! $color ) { $color = 'black'; }

printf(
	'<header style="color: %s;">',
	esc_attr( $color )
);
Hello World blog post with the color of the title determined by fe_color, i.e. the title is red

Link to template code

Gutenberg

 

Custom Field with dropdown with fe_subtitle as selected option and a value of \

Gutenberg

Removes the Custom Fields Metabox

Custom Field with dropdown with fe_subtitle as selected option and a value of \

Restore the Metabox

The Custom Fields for Gutenberg Plugin restores the Custom Fields Metabox.

CMB2

A toolkit for building metaboxes, custom fields

Custom Field and CMB2 color picker (closed) for fe_color

Link to add CMB2 Title Color field

CMB2 color picker open
Custom Field and CMB2 color picker (closed) for fe_color both with updated value, which was entered with the color picker

Link to add CMB2 Title Color field

wp_postmeta

WordPress wp_postmeta as a spreadsheet, now including an updated value for fe_color
Hello World blog post with the color of the title determined by the updated value for fe_color, i.e. the title is blue

CMB2 Field Types Include:

See full list of CMB2 Field Types

wp_postmeta

WordPress wp_postmeta as a spreadsheet

get_post_meta( $post_id, $meta_key, true );

Iron Code Studio

Sal Ferrarello

salcode.com/post-meta

@salcode

Additional Resources

Additional Resources are available at salcode.com/post-meta#resources

Image Credits