Creating a custom post type with custom fields

This is my first wordpress site. I am creating a local community site and I want to have similar functionality to freecycle, that is users can offer things to other people for free. So they’d need to be a drop down box to say if the thing was a wanted or an offered item. A title field for the name of the item. Then details of the item. Upload an image of the item and a location field, where people have to pick it up from. Also possibly a contact number field. Plus ideally, I’d like a checkbox system so that if the item was offered to someone the user could tick a box saying offered and then the post would maybe get assigned a category of awaiting collection and the text of it go grey and disappear out of the list of posts. When it was collected, they could click another box which would then give it a category of archive. I don’t even know if any of this is possible though…

If it is though, how is the best way to do this? Considering I want to be able to use these posts on other areas of the site e.g. in a sidebar showing most recent items offered. I also want to be able to add a minimised version of the form which will sit in the sidebar of certain pages, so the user can add an item whilst on a different page.

Related posts

Leave a Reply

3 comments

  1. firstly, I’d recommend having a go at writing custom post types yourself, they’re not that difficult if you follow the instructions in the WordPress Codex: http://codex.wordpress.org/Function_Reference/register_post_type (The Codex is your best friend when it comes to WordPress development!)

    If you still have trouble, there are a number of plugins which can help with the creation of custom post types, for example: http://wordpress.org/extend/plugins/custom-post-type-ui/

    Lastly, to add custom fields to your custom post types, I’d recommend using a great plugin that I use quite often: http://www.advancedcustomfields.com/ (I’ve found it very reliable and a great time saver as opposed to manually creating custom meta fields)

    I definitely recommend investigating how to create custom post types and custom meta fields yourself – if you don’t have time to do it on this build, then make time on your next one. It’s good to know how to do everything manually rather than relying on plugins.

    Hope that helps!

  2. there is two ways:

    • Easy Way – there is composer package for it – wp_stem

      here is how to create custom post type and add field to it:

      PostType::create('custom_post_name')-> meta_box(array(
        'id'        => 'your_id',
        'title'    => 'your_title'
       ))->field(array(
         'name'      => 'your_field_name',
         'label'     => 'your field label',
         'value'     => 'your_field_default_value',
         'type'      => 'your_field_type'
      ));
      

      how to install and use it see -> github


    • WordPress Way (4 steps)

      1. create post type

      function create_post_type() {
          register_post_type( 'acme',
            array(
              'labels' => array(
                'name' => __( 'acme' )
              ),
              'public' => true,
              'has_archive' => true,
            )
          );
        }
        add_action( 'init', 'create_post_type' );
      

      2. add meta box

      function add_custom_metabox(){
          add_meta_box(
              "your_id",
              "Your Title",
              "custom_metabox_content",
              "post"
          );
      }
      add_action("add_meta_boxes", "add_custom_metabox");
      

      3. add field to meta box content

      function custom_meta_box_content($post_id){
          $value = get_post_meta($post_id, "custom_meta_field", true);
      
          echo '<input type="text" name="custom_meta_field" value="'. $value                                             .'">';
       }
      

      4. listen to save post hook and update value(when you click update post from admin panel).

      function on_save_post($post_id){
          $meta_value = isset($_POST["custom_meta_field"]) ?  $_POST["custom_meta_field"]  : false;
      
          if (current_user_can("edit_posts") && $meta_value){
              update_post_meta($post_id,"custom_meta_field", $meta_value);
          }
       }
       add_action("save_post", "on_save_post");
      
  3. Use this code in functions.php

    function foobar_func( $atts )
    { 
       global $post; 
       $key=$atts['name']; 
       if(empty($key)) return; 
       return get_post_meta($post->ID, $key, true); 
    } 
    
    add_shortcode( 'foobar', 'foobar_func' );  
    

    And for display

    [foobar name=custom_field_name]  
    

    in your post page.