WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking to create a dropdown in the post edit screen which contains 3 already existing tags. What's the easiest way of doing this?

Basically all I'm looking for is a simple drop down which adds one of the following tags to the post itself; 'beef-stew', 'pea-soup' & 'chili'. I'd also like 'beef-stew' to be the default.

Thank you in advance

EDIT: As I don't want the user to be able to display more than 1 of these 3 categories at any time, and have to option to easily change which one as they please, tags might not be the best solution? Would I be better off creating a custom taxonomy ('food')? They will basically be used to change the way a post is displayed on the front page of the website.

share|improve this question
up vote 0 down vote accepted

I followed this handy guide and it worked a treat:

http://shibashake.com/wordpress-theme/wordpress-custom-taxonomy-input-panels

share|improve this answer

Create Custom Post Type

function create_product()
{
   $labels = array(
    'name'               => _x( 'Product', 'post type general name', 'stacy' ),
    'singular_name'      => _x( 'product', 'post type singular name', 'stacy' ),
    'menu_name'          => _x( 'Products', 'admin menu', 'stacy' ),
    'name_admin_bar'     => _x( 'Product', 'add new on admin bar', 'stacy' ),
    'add_new'            => _x( 'Add New', 'product', 'stacy' ),
    'add_new_item'       => __( 'Add New Product', 'stacy' ),
    'new_item'           => __( 'New Product', 'stacy' ),
    'edit_item'          => __( 'Edit Product', 'stacy' ),
    'view_item'          => __( 'View Product', 'stacy' ),
    'all_items'          => __( 'All Product', 'stacy' ),
    'search_items'       => __( 'Search Product', 'stacy' ),
    'not_found'          => __( 'No Product found.', 'stacy' ),
    'not_found_in_trash' => __( 'No Product found in Trash.', 'stacy' )
);
   $args = array(
    'labels'             => $labels,
    'description'        => __( 'Description.', 'Add New Product on stacy' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'product' ),
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => 100,
            'menu_icon'          =>'dashicons-cart',
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail','comments','capabilities' ),
            'taxonomies'         => array('product_category','product_tag')
);
    register_post_type( 'product', $args );
 }
 add_action( 'init', 'create_product' );

Create Custom Metabox and Display meta value on edit post

  function add_product_details_meta_box()
  {
   global $wpdb;
   global $post;
   $custom = get_post_custom( $post->ID );
   <p>
    <label>Short Description:</label><br />
    <textarea rows="5" name="short_description" class="width99"><?= @$custom["short_description"][0] ?></textarea>
   </p> 
   <p>
    <label>Price:</label><br />
            <input type="text" name="price" value="<?= @$custom["price"][0] ?>" class="width99" />
   </p>

    <p>
    <label>Dimensions (in):</label><br />
            <input type="text" name="length" value="<?= @$custom["length"][0] ?>" class="s" placeholder="Length"/>
    </p>
    <p>
    <label>Shipping Lead Days:</label><br />
            <input type="text" name="ship_lead_days" value="<?= @$custom["product_ship_lead_days"][0] ?>" class="s" placeholder="Shipping Lead Days"/>
    </p>
    <p>
    <label>Commision:</label><br />
            <input type="text" name="commision_broker" value="<?= @$custom["commision_broker"][0] ?>" class="s" placeholder="Enter Your Commision Here"/>
    </p>
   }
  add_action( 'admin_init', 'add_product_meta_boxes' );

Update Post Meta

function save_product_custom_fields(){
 global $post;

 if ( $post )
 {
   update_post_meta($post->ID, "short_description", @$_POST["short_description"]);
  update_post_meta($post->ID, "price", @$_POST["price"]);
  update_post_meta($post->ID, "length", @$_POST["length"]);
       update_post_meta($post->ID,'product_ship_lead_days',@$_POST['ship_lead_days']);
 update_post_meta($post->ID,'commision_broker',@$_POST['commision_broker']);
  }
}
add_action( 'save_post', 'save_product_custom_fields' );
share|improve this answer
    
Can you just explain a little bit in addition to code – bravokeyl Jul 14 at 9:41
    
first code for create a custom post type second code for meta box that you want to some custom fields on your post type. – Vivek Jul 14 at 10:02
    
last code for update meta box value on post meta – Vivek Jul 14 at 10:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.