Site icon DsgnWrks

Post Meta.. How do I save it? (add_post_meta)

This is Part 3 of a five-part series on working with WordPress post-meta. If you haven’t, you’ll probably want to start with Part 1.

We managed to accomplish our task of saving the data for the first three using the handy WordPress-provided function, update_post_meta. However, for the last bit of data, we want to store that data in a slightly different way.

Since we want the ability to store multiple URLs for one post meta key, we’ll use the function add_post_meta instead. As suspected, this function allows us to continually add more entries to the post meta field.
Let’s dive in to code:

$post_id = 25;
$meta_key = 'wpsnipp_post_extra_resource_links';
// Add one resource
$meta_value = 'http://google.com';
add_post_meta( $post_id, $meta_key, $meta_value );

// And another
$meta_value = 'http://amazon.com';
add_post_meta( $post_id, $meta_key, $meta_value );

// No resource list would be complete without wpsnipp.com
$meta_value = 'http://wpsnipp.com';
add_post_meta( $post_id, $meta_key, $meta_value );

Ok! Hopefully it’s obvious what we’ve accomplished, but let’s review. First we set up our $post_id and $meta_key variables. We created these variables first because they’ll be re-used for each add_post_meta call.

Next, we add 3 different values to our post meta field. So that’s
great, but how do we retrieve those values and verify that it’s all
working. If we used get_post_meta( $post_id, $meta_key, true ); like in our last example, what do you think would be the result? Well,
we would just get the first row’s value, ‘http://google.com’. Obviously,
that was not our intention, but rather to retrieve a list of resources
that we could then display in our theme, possibly in an unordered list
in our sidebar, or below the content. So let’s make that happen.

// ... Later in your theme or plugin
// This retrieves current ID if we're in the loop
$post_id = get_the_ID();
$meta_key = 'wpsnipp_post_extra_resource_links';

// Now, let's retrieve our list of resources
$resources = get_post_meta( $post_id, $meta_key );

Notice that the third parameter is ommitted. That’s because the third parameter in get_post_meta is set to ‘false’ by default, so since that is what we want, we can simply ommit the last parameter.

This will then return an ‘array’ of resources. If you’re not familiar
with arrays, just know that it is the list that we are looking for, and
we will need to iterate over that list via a loop, a term you have
undoubtedly heard, most often in reference to the WordPress posts loop.
For our use-case we will use a foreach loop, which simply
means, for each item in our list (array), do the following (followed by
your code). So we’ll do exactly that, loop through the resources and
create an unordered list.

// Let's make sure we have some resources before we start
if ( $resources ) {
    ?>
    <ul class="extra-resources">
    <?php
    foreach ( $resources as $link ) {
        ?>
        <li><a href="<?php echo esc_url( $link ); ?>"><?php echo esc_html( $link ); ?></a></li>
        <?php
    }
    ?>
    </ul>
    <?php
}

Let’s review. We first grabbed our list of resources by using get_post_meta,
and ommitting the third parameter. We then checked to verify that we
did, in fact, have some resources. We did this because there is no sense
in outputting an empy <ul></ul> tag if we don’t have any list items to fill it with. Inside our conditional, we have output the opening <ul> tag and begun our loop. As the foreach loop iterates across the array, it places each item into a variable we called $link. We then ouput our link tag, <a href=… wrapped in the <li> tags.

In the last article, I mentioned the importance of sanitization.
Sanitization is the act of cleaning our data before input, or saving it
to the database. Another equally important aspect of passing around
data, is the act of “Escaping” or cleaning our data before output. In the above example, I have used 2 functions which WordPress provides to help escape our data before display, esc_url, and esc_html. The former, esc_url is self-explanatory in that it makes sure the data being shown is a
proper url, and even verifies that the ‘http://’ is attached to the
beginning. The second, esc_html removes any html in the
data being displayed. In our case, we’re just displaying the url, so we
know we don’t want any html tags in there.

Ok, so it’s that easy to save multiple sources of information to the
same meta key and then to display it. There are some other caveats to
working with arrays of data and the post meta functions, but we’ll save
those for another article some other time. What kinds of data will you
be saving and displaying?

In the next article, we’ll walk you through a simple example of saving post meta data each time a post is saved.

Part 3 of a 5 part series originally written for wpsnipp.com (1, 2, 3, 4, 5)

Exit mobile version