Created
July 5, 2011 21:05
-
-
Save mattboon/1065937 to your computer and use it in GitHub Desktop.
WordPress CPT with Taxonomy Custom Fields II (Not quite working)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//---- CUSTOM POST TYPE - FAQs -------------------------------------------------------- | |
add_action('init', '_init_question_post_type'); | |
function _init_question_post_type() { | |
// USED FOR CUSTOM TAXONOMY FIELDS | |
global $wpdb; | |
// MAKE POST TYPE | |
//--------------------------- | |
register_post_type( 'question', | |
array( | |
'labels' => array( | |
'name' => __( 'FAQs' ), | |
'singular_name' => __( 'FAQ' ), | |
'add_new' => __( 'Add New' ), | |
'add_new_item' => __( 'Add New FAQ' ), | |
'edit' => __( 'Edit' ), | |
'edit_item' => __( 'Edit FAQ' ), | |
'new_item' => __( 'New FAQ' ), | |
'view' => __( 'View FAQ' ), | |
'view_item' => __( 'View FAQ' ), | |
'search_items' => __( 'Search FAQ' ), | |
'not_found' => __( 'No FAQs found' ), | |
'not_found_in_trash' => __( 'No FAQs found in Trash' ), | |
), | |
'public' => true, | |
'show_ui' => true, | |
'publicly_queryable' => true, | |
'exclude_from_search' => false, | |
'hierarchical' => false, | |
'menu_position' => 20, | |
'capability_type' => 'post', | |
'menu_icon' => '/assets/images/faq_menu.png', | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'about/faqs', 'with_front' => false ), | |
'supports' => array('title', 'editor'), | |
) | |
); | |
// MAKE PERMALINKS WORK | |
//--------------------------- | |
flush_rewrite_rules(); | |
// MAKE TAXONOMY | |
//--------------------------- | |
register_taxonomy_for_object_type('question', 'question-category'); | |
register_taxonomy( | |
'question-category', | |
array( 'question' ), | |
array( | |
'labels' => array( | |
'name' => __( 'FAQ Categories' ), | |
'singular_name' => __( 'FAQ Category' ), | |
'search_items' => __( 'Search FAQ Categories' ), | |
'popular_items' => __( 'Popular FAQ Categories' ), | |
'all_items' => __( 'All FAQ Categories' ), | |
'parent_item' => __( 'Parent FAQ Category' ), | |
'parent_item_colon' => __( 'Parent FAQ Category:' ), | |
'edit_item' => __( 'Edit FAQ Category' ), | |
'update_item' => __( 'Update FAQ Category' ), | |
'add_new_item' => __( 'Add New FAQ Category' ), | |
'new_item_name' => __( 'New FAQ Category' ), | |
), | |
'public' => true, | |
'show_in_nav_menus' => true, | |
'show_ui' => true, | |
'publicly_queryable' => true, | |
'exclude_from_search' => false, | |
//'rewrite' => array( 'slug' => 'topics', 'with_front' => false ), | |
'hierarchical' => true, | |
'query_var' => true, | |
) | |
); | |
// USE CUSTOM DB TABLE - NO HYPHENS! | |
$wpdb->{question-categorymeta} = $wpdb->prefix."question_categorymeta"; | |
// DEFINE CUSTOM TAXONOMY FIELDS | |
//--------------------------- | |
add_action( 'question-category_edit_form_fields', 'question_define_custom_fields', 10, 2); | |
function question_define_custom_fields($tag, $taxonomy) | |
{ | |
// Get column value | |
$question_cat_column = get_metadata($tag->taxonomy, $tag->term_id, 'question_cat_column', true); | |
// Check / set column value | |
if (!$question_cat_column) $question_cat_column = "Left"; | |
// Get menu order value | |
$question_cat_menu_order = get_metadata($tag->taxonomy, $tag->term_id, 'question_cat_menu_order', true); | |
?> | |
<?php print_r($tag->taxonomy); ?> | |
<?php print_r($tag->term_id); ?> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="question_cat_column">Column</label></th> | |
<td> | |
<select style="width: 100px;" name="question_cat_column" id="question_cat_column"> | |
<option value="Left" selected="selected">Left</option> | |
<option value="Right" <?php | |
if ($question_cat_column == "Right") echo ' selected="selected";'; ?>>Right</option> | |
</select> | |
<!--p class="description">Marking a Package to 'No' will hide all items in that package.</p--> | |
</td> | |
</tr> | |
<tr class="form-field"> | |
<th scope="row" valign="top"><label for="question_cat_menu_order">Order</label></th> | |
<td> | |
<input style="width: 98px;" maxlength="2" type="text" name="question_cat_menu_order" id="question_cat_menu_order" | |
value="<?php echo $question_cat_menu_order; ?>"/><br /> | |
<!--p class="description">This is the unit price for the items in this 'package'.</p--> | |
</td> | |
</tr> | |
<?php | |
} | |
// SAVE CUSTOM TAXONOMY FIELDS | |
//--------------------------- | |
add_action( 'edited_question-category', 'save_custom_fields', 10, 2); | |
function save_custom_fields($term_id, $tt_id) | |
{ | |
if (!$term_id) return; | |
if (isset($_POST['question_cat_column'])) | |
update_metadata($_POST['taxonomy'], $term_id, 'question_cat_column', | |
$_POST['question_cat_column']); | |
if (isset($_POST['question_cat_menu_order'])) | |
update_metadata($_POST['taxonomy'], $term_id, 'question_cat_menu_order', | |
sprintf("%01.2f", $_POST['question_cat_menu_order'])); | |
} | |
// DEFINE CUSTOM COLUMNS | |
//--------------------------- | |
add_filter("manage_edit-question_columns", "question_define_columns"); | |
function question_define_columns($question_columns) { | |
$new_columns['cb'] = '<input type="checkbox" />'; | |
$new_columns['title'] = __('Question'); | |
$new_columns['question-category'] = __('Category'); | |
//$new_columns['question-answer'] = __('Answer'); | |
//$new_columns['author'] = __('Author'); | |
//$new_columns['comments'] = __('Comments'); | |
//$new_columns['tags'] = __('Tags'); | |
//$new_columns['date'] = __('Date'); | |
//$new_columns['hsm_pagetitle'] = __('Page title'); | |
//$new_columns['hsm_pagedescription'] = __('Page description'); | |
return $new_columns; | |
} | |
// POPULATE CUSTOM COLUMNS | |
//--------------------------- | |
add_action('manage_question_posts_custom_column', 'question_fill_columns', 10, 2); | |
function question_fill_columns($column_name, $id) { | |
global $post; | |
switch ($column_name) { | |
case 'question-category': | |
echo strip_tags(get_the_term_list( $id, 'question-category', '', ', ', '' )); | |
break; | |
default: | |
break; | |
} | |
} | |
// SORTABLE COLUMN HEADERS | |
//--------------------------- | |
add_filter("manage_edit-question_sortable_columns", 'question_sortable_columns'); | |
function question_sortable_columns($columns) { | |
$custom = array( | |
'question-category' => 'question-category', | |
); | |
return wp_parse_args($custom, $columns); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment