I am trying to build an application that takes 2 pieces of input from a user and saves it to the database.
I have tried Custom Post Type, but its concept is foreign and confusing to me – I don’t know if I should use pre-existing fields (Title, Content) or create the fields to store the input in the database.
Here is my code:
// Register Custom Post Type
function int_init() {
$labels = array(
'name' => 'Stories',
'singular_name' => 'Story',
'menu_name' => 'Story',
'parent_item_colon' => 'Parent Stories:',
'all_items' => 'All Stories',
'view_item' => 'View Story',
'add_new_item' => 'Add new Story',
'add_new' => 'New Story',
'edit_item' => 'Edit Story',
'update_item' => 'Update Story',
'search_items' => 'Search Story',
'not_found' => 'No story found',
'not_found_in_trash' => 'No story found in Trash',
);
$args = array(
'label' => 'int',
'description' => 'INT information pages',
'labels' => $labels,
'supports' => array( 'title', 'editor', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => '',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'int', $args );
}
// Hook into the 'init' action
add_action( 'init', 'int_init', 0 );
All this code does for me is show the Title and Content fields in the backend. Should I just use the Title and Content fields, or should I create new fields? How do I do this?
EDIT: The content is unique per user inasmuch every user will be asked to enter two pieces of alpha numeric data per submission using a form created using shortcode. I know how shortcodes work and have used them in the past.
The pair of data items entered will be saved to the database, and will need to be retrieved and posted on the public side of the site. The pair of data items will be retrieved and displayed on one page for each data pair as such:
I want to eat a peanut butter sandwich then drink a glass of milk. The submitted data is shown as bold. This retrieval and formatting of the data will be done using shortcode. I do not know how to create custom fields to make this work. I understand how Custom Post Types works on a basic level, but I do not understand how to use Custom Post Types to make my project work.
The data is submitted on the public side of the site by users who are not logged into the site.
To recap my problem, I don’t know how to store the pair of data. Do I use pre-existing Title and Content fields or do I create something different?
A custom post type is exactly that – a custom post type. WordPress uses regular, built-in posts to display in the blog portion of a website. You create them in the ‘posts’ button you see in the backend. So what you did was add a new, custom post type. That creates a new button with label ‘int’. But the types of input will be the same as a regular post type – you’ll see the tinyMCE box, apply category/tag, etc.
I think that apart from a custom post type, what you need are custom fields as expressed on your image. An easy and convenient way to deal with those would be to use this plugin: http://www.advancedcustomfields.com/
Or, if you don’t really need a custom post type – which will maybe make your loops slightly harder since you’ll have to filter what gets queried in order to get what you really want each time – maybe you could do on custom fields alone.
If you need the user to have only a couple input boxes, no tinyMCE etc though, I’d say you’ll need both custom types and custom fields.
Edit: more info about custom post types here: Post Types « WordPress Codex
and custom fields here: Custom fields « WordPress Codex
EDIT: If the user will enter these 2 ‘inputs’ every time they post something, that is, if those 2 snippets will exist in a per post basis… I think you are definitely looking for the functionality custom fields give. Forget about custom post types. Simply create custom fields and retrieve them through the theme like so, in a single post loop:
I’m still not sure if that’s what you meant though 🙂