{"id":1108,"date":"2017-12-12T17:48:23","date_gmt":"2017-12-12T17:48:23","guid":{"rendered":"https:\/\/firstsiteguide.com\/how-to-use-custom-fields-in-wordpress\/"},"modified":"2023-10-04T11:43:39","modified_gmt":"2023-10-04T11:43:39","slug":"wordpress-custom-fields","status":"publish","type":"post","link":"https:\/\/firstsiteguide.com\/wordpress-custom-fields\/","title":{"rendered":"How to Use Custom Fields in WordPress","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"

While you were writing your previous article, you might have scrolled over the Custom Fields area in the post editor.<\/p>\n\n\n\n

This simple, yet so powerful WordPress feature, might go unnoticed by beginners but it\u2019s actually a feature which can undoubtedly transform your simple and plain website into a dynamic portal<\/strong> which will attract new visitors.<\/p>\n\n\n\n

Let us show you more about custom fields and how to use them on your site.<\/p>\n\n\n\n

What are Custom Fields and how do they work<\/h2>\n\n\n\n

What are exactly custom fields? These little fields allow any user to enter extra information about the post<\/strong>. This is known as meta-data, i.e. \u201cdata about data\u201d and allows you to attach additional information to your posts. Values you can enter in custom fields can range from something simple as adding a text or a number to your post, adding images, changing styles through the field, or actually doing anything that goes through your mind.<\/p>\n\n\n

\n
\"Custom<\/figure><\/div>\n\n\n

<\/p>\n\n\n\n

Maybe everything will be clearer with an example.<\/p>\n\n\n\n

Imagine that you\u2019re running a website dedicated to video games<\/strong>. You write news and publish trailers, play around with gaming hardware so you can talk about it and of course you write reviews after new games arrive. After you have written a review and placed screenshots and gameplay videos in the article, you probably want to display important information about the game.<\/p>\n\n\n\n

Instead of hiding the most important information about a game in the lengthy text, you might want to display a game\u2019s score, developer and publisher names linking to their website, release date, etc. Yes, custom fields are something that can help you do the magic.<\/p>\n\n\n\n

If you let your authors enter that information in a custom field, you can easily extract the information and display it in addition to the review<\/strong>. You\u2019ve seen infoboxes on review sites. If you do so, all reviews can have a standardized template \u2013 this can improve user experience, both on the front and the backside.<\/p>\n\n\n\n

Following the gaming website example, you would end up having several custom fields which your authors can enter and which you show to your readers:<\/p>\n\n\n

\n
\"Call<\/figure><\/div>\n\n\n

<\/p>\n\n\n\n

This is the information your authors would enter into custom fields, and you can display the same in a post. But how?<\/p>\n\n\n\n

Display all custom fields:<\/h3>\n\n\n\n

If you only have one field or you want to show all of your fields on the same spot, you can hook you meta-data all at once with a simple function:<\/p>\n\n\n\n

    \n
  1. Open single.php<\/li>\n\n\n\n
  2. Find the_content () function and paste this code before or after it (depending on where you want to display custom fields):<\/li>\n<\/ol>\n\n\n
    \n \n\n
    <?php the_meta(); ?><\/pre>\n\n<\/div>\n\n\n
    <\/pre>\n\n\n\n
      \n
    1. Save changes<\/li>\n<\/ol>\n\n\n\n

      Display specific custom field:<\/h3>\n\n\n\n

      That\u2019s it. That simple function will now stay hooked and it will show all custom fields from a post. But what if you want to show different field in a different position<\/strong> or if you only want to show one specific key? Then you will use a slightly different approach and define the key which will be displayed:<\/p>\n\n\n\n

        \n
      1. Open single.php file<\/li>\n\n\n\n
      2. Find the content and c\/p the following:<\/li>\n<\/ol>\n\n\n
        \n \n\n
        <?php echo get_post_meta($post->ID, 'key', true); ?><\/pre>\n\n<\/div>\n\n\n
        <\/pre>\n\n\n\n
          \n
        1. Replace \u2018key\u2019 with the actual custom field you are using in a post. For example: \u2018Publisher\u2019<\/li>\n\n\n\n
        2. Save changes<\/li>\n<\/ol>\n\n\n\n

          This function will check each and every post in the loop and search for a custom field named \u201cPublisher\u201d. If the key was found, its value will be displayed<\/strong>. You can further customize the way a custom field is being displayed by adding a class to it and style the class with CSS or you can directly enter HTML into the field value.<\/p>\n\n\n\n

          Conditional Custom Fields:<\/h3>\n\n\n\n

          If you want to, you can use custom fields as conditional. Let stay with the example and let\u2019s say there are times that you won\u2019t be able to know a release date for the game you\u2019re writing about. Instead of typing a custom field, you can automatize the process and tell WordPress to write \u201cTBA\u201d (to be announced) into Release Date field if there isn\u2019t any data added by the user:<\/p>\n\n\n

          \n \n\n
          <?php $release_date = get_post_meta($post->ID, \n'Release Date', true);\nif ($release_date) {\n?>\n<?php echo $release_date; ?>\n<?php } else { ?>\n<p>To Be Announced.<\/p>\n<?php } ?><\/pre>\n\n<\/div>\n\n\n
          <\/pre>\n\n\n\n

          OK, we\u2019ll wrap it up from here. This is the most basic way of using custom fields. But now that you have covered the basics, you can actually do pretty much anything with them. You can, for example, show your mood or a song you\u2019re listening to. Or, you can show a list of posts which contain specific key and\/or value. The sky is the limit.<\/p>\n\n\n\n

          How to list posts with a specific custom field<\/h2>\n\n\n\n

          Custom fields are a great way of extending your posts\u2019 functionality. Instead of letting users deal with the code, you can let them enter a value in a custom field and do something with that information \u2013 there are endless possibilities.<\/p>\n\n\n\n

          Sometimes, when you are already using specific custom fields, you may need to list posts with those fields.<\/p>\n\n\n\n

          For example, if you are writing game reviews and already have different custom fields where you display publisher, genre, score, etc. in a separate information box in your review, you might want to display only games from that specific publisher, games from FPS genre or only those reviews with a score of 10<\/strong>.<\/p>\n\n\n\n

          This kind of information could really be interesting to your visitor. You can allow them to browse your game reviews in much more details or allow them to see the top 10 rated games on your site<\/strong>. Even if you don\u2019t want to publish a new list, this method can help you with data mining \u2013 instead of creating an article from it, you can list all posts with certain property for research purposes only.<\/p>\n\n\n\n

          In our opinion, the best way of listing posts sorted by a custom field is by creating a new page template and showing a list on a new page<\/strong>. This method allows you to organize your pages and to easily use a new page for displaying what you need. In the following lines, we are about to show you how to find all posts with a specific custom field and\/or its value and show it on a new page.<\/p>\n\n\n\n

          1. Create a custom template:<\/h3>\n\n\n\n

          Let\u2019s stay with the game review example where we want to display all games from Bethesda Softworks (did anyone say Fallout?). For starters, you will need to create and prepare a custom template:<\/p>\n\n\n\n

            \n
          1. Create a custom page template and name it bethesda-games.php<\/li>\n\n\n\n
          2. Copy and paste the following code into the file:<\/li>\n<\/ol>\n\n\n
            \n \n\n
            <?php
            \/*
            Template Name: Bethesda games
            *\/
            ?>
            <?php get_header(); ?>
            <?div class=\"container\">
            <?div class=\"indent\">
            <?php
            query_posts(array(
            'meta_key' => 'publisher\u2019,
            'meta_value' => 'Bethesda'
            ));
            \t\t\t\t
            if ( have_posts() ) while ( have_posts() ) : the_post();
            echo '<?li>';
            the_title();
            echo '<?\/li>';
            endwhile;
            \t\t\t\t
            wp_reset_query(); ?>\t\t
            <?\/div>
            <?\/div>
            <?php get_footer(); ?><\/pre>\n\n<\/div>\n\n\n
            <\/pre>\n\n\n\n
              \n
            1. Save changes<\/li>\n<\/ol>\n\n\n\n

              The code is ready to go through all custom fields and to find only \u2018Publisher\u2019 keys with the value of \u2018Bethesda\u2019. You can change the key and value to anything you want. If you would like to display all posts with \u2018Publisher\u2019 custom field containing any value, simply remove \u2018meta_value\u2019 row entirely.<\/p>\n\n\n\n

              Don\u2019t forget to change the title of the template to something you will recognize later.<\/p>\n\n\n\n

              2. Add the page:<\/h3>\n\n\n\n

              Now your template is ready so you can continue creating the list:<\/p>\n\n\n\n

                \n
              1. Navigate to Pages->Add New<\/em><\/li>\n\n\n\n
              2. Add a title<\/li>\n\n\n\n
              3. On the right side, find \u201cPage Attributes\u201d<\/li>\n\n\n\n
              4. Select your template from the \u201cTemplate\u201d dropdown list<\/li>\n\n\n\n
              5. Publish the page<\/li>\n<\/ol>\n\n\n\n

                That\u2019s it! Now if you open your new page, you can see the list of all your posts which contain custom fields with the values you have chosen.<\/p>\n\n\n\n

                Now you have to add some text to your new page, style the list and find the best way to show it to your visitors. We hope that this method will help you sort your posts on a whole other level and that you will enjoy it.<\/p>\n\n\n\n

                Show sidebar by using custom fields<\/h2>\n\n\n\n

                A sidebar was introduced in WordPress version 2.2. Since then, it has become a popular way of showing extra content<\/strong> and more often than not, WordPress themes<\/a> will have one by default where you can place any widgets you want.<\/p>\n\n\n\n

                Although very useful, you don’t want your sidebar to be present everywhere<\/strong>. Usually, these bars are shown anywhere but the home page. And there is no such option to easily turn the sidebar on and off.<\/p>\n\n\n\n

                So, why not make it yourself. In this part of the article, we will show you how to put your sidebar into a custom field and call it only for posts you want.<\/p>\n\n\n\n

                1. Prepare the template:<\/h3>\n\n\n\n
                  \n
                1. Open page.php or single.php file<\/li>\n\n\n\n
                2. Find the following code: <?php get_sidebar(); ?><\/li>\n\n\n\n
                3. Replace the code with this one:<\/li>\n<\/ol>\n\n\n
                  \n \n\n
                  <?php $sidebar = get_post_meta($post->ID, \n\"sidebar\", true);\nget_sidebar($sidebar);\n?><\/pre>\n\n<\/div>\n\n\n
                  <\/pre>\n\n\n\n
                    \n
                  1. Save changes<\/li>\n\n\n\n
                  2. Create a custom template where your sidebar will be<\/li>\n\n\n\n
                  3. Open the sidebar.php file<\/li>\n\n\n\n
                  4. Copy everything from sidebar.php and paste into your custom template file<\/li>\n\n\n\n
                  5. Save changes.<\/li>\n<\/ol>\n\n\n\n

                    Now you have to open a post or a page where you can find “custom fields” under the Text\/Visual Editor. To add a sidebar to that post\/page, do the following.<\/p>\n\n\n\n

                    2. Using the sidebar in custom fields:<\/h3>\n\n\n\n