Last Updated: June 11, 2021
·
8.353K
· austinginder

WordPress Custom Post Type with Custom Fields

In functions.php

/*  Being custom post types */

add_action('init', 'pdf_resources_register');
function pdf_resources_register() {

    $labels = [
        'name'               => _x('PDF Resource', 'post type general name'),
        'singular_name'      => _x('PDF Resource', 'post type singular name'),
        'add_new'            => _x('Add New', 'PDF Resource'),
        'add_new_item'       => __('Add New PDF Resource'),
        'edit_item'          => __('Edit PDF Resource'),
        'new_item'           => __('New PDF Resource'),
        'view_item'          => __('View PDF Resource'),
        'search_items'       => __('Search PDF Resources'),
        'not_found'          =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon'  => ''
    ];

    $args = [
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'query_var'          => true,
        'rewrite'            => true,
        'capability_type'    => 'post',
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => [ 'title', 'editor', 'thumbnail' ],
        'rewrite'            => [ 'slug' => 'pdf_resource' , 'with_front' => FALSE ]
      ];

    register_post_type( 'pdf_resource' , $args );
}

add_action("admin_init", "admin_init");

function admin_init(){
  add_meta_box("pdf-meta", "Upload", "pdf_meta", "pdf_resource", "side", "low");
}

function pdf_meta() {
  global $post;
  $custom   = get_post_custom($post->ID);
  $pdf_file = $custom["pdf_file"][0];
  echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  ?>
  <p><label>PDF File:</label><br />
  <input type="text" name="pdf_file" id="pdf_file" value="<?php echo $pdf_file; ?>" style="width:258px">
  <input type="button" name="pdf_button" id="pdf_button" value="Browse" style="width:258px"></p> 
  <?php
}

register_taxonomy( "pdf_category", [ "pdf_resource" ], [ "hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true ] );
register_taxonomy( "pdf_color", [ "pdf_resource" ], [ "hierarchical" => true, "label" => "Colors", "singular_label" => "Color", "rewrite" => true ] );

function my_admin_scripts() {
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
    wp_register_script('my-upload', get_bloginfo('template_url') . '/js/my-script.js', [ 'jquery', 'media-upload', 'thickbox' ] );
    wp_enqueue_script('my-upload');
} 

function my_admin_styles() {
    wp_enqueue_style('thickbox');
}

add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');

add_action('save_post', 'save_details');
function save_details(){
    global $post;

    if( $post->post_type == "pdf_resource" ) {
        update_post_meta($post->ID, "pdf_file", $_POST["pdf_file"]);
    }

}
/*  End custom post types */

In js/my-script.js I have the following which allows me to upload a file directly to the custom post type.

jQuery(document).ready(function() {
     jQuery('#pdf_button').click(function() {
         window.send_to_editor = function(html) {
          imgurl = jQuery(html).attr('src') || jQuery(html).find('img').attr('src') || jQuery(html).attr('href');
          jQuery('#pdf_file').val(imgurl);
         tb_remove();
      }
      tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
      return false;
     });
});

In index-fact-sheets.php I display the PDF in various colors. To see the live version of this check out Fact Sheets and Handouts

<?php
/*
Template Name: Fact Sheets Template
*/
?><?php get_header(); ?>
<div id="container">

        <div id="mainbar">

            <h1>Fact Sheets</h1>

            <?php 
              $temp                = $wp_query;
              $paged               = (get_query_var('paged')) ? get_query_var('paged') : 1;
              $post_per_page = 100; // -1 shows all posts
              $args = [
                'post_type'      => 'pdf_resource',
                'orderby'        => 'date',
                'order'          => 'ASC',
                'paged'          => $paged,
                'pdf_category'   => 'fact-sheets',
                'posts_per_page' => $post_per_page
              ];
              $wp_query = new WP_Query($args); 

              if( have_posts() ) : while ($wp_query->have_posts()) : $wp_query->the_post();
                  $custom           = get_post_custom($post->ID);
                  $pdf_file         = $custom["pdf_file"][0];
                  $termsObjects     = wp_get_object_terms($post->ID, 'pdf_color');
                  $currentCustomCat = $termsObjects[0]->slug;
             ?>

            <div class="post-block<?php 
                if ($currentCustomCat == "teal") { echo " teal"; }
                if ($currentCustomCat == "orange") { echo " orange"; }
                if ($currentCustomCat == "green") { echo " green"; }
                if ($currentCustomCat == "light-orange") { echo " ltorange"; }
                if ($currentCustomCat == "light-green") { echo " ltgreen"; }
                if ($currentCustomCat == "red") { echo " red"; }
                ?>"> 
                <a href="<?php if ($pdf_file) { echo $pdf_file; } else { echo "#"; } ?>"><?php the_title(); ?><span>Download PDF</span></a>
                <?php edit_post_link('Edit', '<div class="post-edit-link">', '</div>'); ?>
            </div>
            <?php endwhile; else: ?>
            <?php endif; wp_reset_query(); $wp_query = $temp; the_post(); ?>
        </div>

        <div id="sidebar">
            <ul>
                <?php if ( is_active_sidebar( 'sidebar-widget-area' ) ) : ?>
                    <?php dynamic_sidebar( 'sidebar-widget-area' ); ?>
                <?php endif; ?>
            </ul>
        </div>
</div>
<?php get_footer(); ?> 

7 Responses
Add your response

Hi Austin!

I've stumbled over your post oven and consider it as very clear and helpful.

I followed the steps and included the code and files in a wp test-site using the standard twentyeleven theme.

Nevertheless the (coloured) PDF-links don't appear on my fact sheet page. Is there anything else to consider using these PDF Resource post types???

Thanks a lot for your advice

Best regards

Marc

over 1 year ago ·

@marcez Marc,

There could be a couple reasons for this. One thing I didn't mention was the css styling I was using. Without the proper css the links won't appear colored. Another thing to look at is the categories. In my "index-fact-sheets.php" I'm specifically targeting the post that have the pdf_category set to 'fact-sheets'. In your backend go to "PDF Resource" and select "Categories". Make sure you have a something in the respective categories. Here is what my backend screen looks like: http://cl.ly/image/0G1C1R2b3L3F. If you still have problems, feel free to contact me directly.

Austin

over 1 year ago ·

Hi again!

Thanks a lot for the quick answer!!!
I've got it working with your helpful advise.
Had a mistake in the category (slug-name)...

Issue solved :)

Kind regards

Marc

over 1 year ago ·

@marcez Sure thing. Glad I was able to help.

over 1 year ago ·

Since the only thing I'm storing is the full url to the file, you would have to use some fancy PHP to extract the file name and file size from that.

This solution is a bit outdated now. I would recommend using Advanced Custom Fields plugin: http://www.advancedcustomfields.com/. ACF is powerful. I use it on every website I build. When using ACF with files, look here for a code example: http://www.advancedcustomfields.com/resources/field-types/file/. With this example you would do something like:

// retrieve file size
echo filesize( get_attached_file( get_field('file')) );

// retrieve file name
echo basename ( get_attached_file( get_field('file')) );
over 1 year ago ·

Thanks for this great tutorial!
I would like to read add the category to the url to display using just this one template:
In other words replace:
'pdfcategory' => 'fact-sheets',
with
pdf
category = // get from url
like mysite.com/pdfs/fact-sheets

How would I go about that?

over 1 year ago ·

This article is a bit outdated. Now of days, I use ACF to generate custom fields + http://generatewp.com/post-type/ to generate the custom post type.

To answer your question, instead of using a page template you could adapt the code into a taxonomy page. Maybe taxonomy-pdf_category.php. Then in that page you could let WordPress setup the loop for you by using a standard loop like so.

<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>


<?php endwhile; ?>
over 1 year ago ·