So, I'm working on porting a self-correcting exam from PHP/SQL to wordpress. I've got two custom post types, question and answer, to hold the data for the exam, and two custom taxonomies, each registered to both post types, to help in structuring and organization.
The idea is, the "Exam" taxonomy is used to mark every question and answer that belong in the same exam. That way, multiple exams can be handled. And the "Exam Item" taxonomy is used to mark questions and answers that belong as one unit on an exam. Each post type also has one custom meta field.
So, I've registered my taxonomies and post types, hooked them as init actions. I can see/edit the taxonomies from the dashboard, and I've put a number of questions and answers in the database for testing.
I've also put custom columns up for my new post types, to display their taxonomy and meta data. And, I've put on the admin settings page, a clickable list of all the active "Exam" terms, so that it can display a sort of answer key for a quick look at how each exam will be displayed when it is taken.
All of that works great. The problem is, when you go to see the admin key for an exam, it doesn't think my "exam item" taxonomy exists, and therefore I can't WP_Query the answers that belong with the questions. I've tried a whole bunch of different get_term related functions, tried referencing the taxonomy by name, slug, description, label, etc, and all I get is WP_ERROR.
Even taxonomy_exists is telling me that there is no such taxonomy, but I KNOW there is, because its displaying on the custom post types and referencing fine there.
The only suggestions I could find in previously existing posts are that I'm trying to check whether the taxonomy exists BEFORE registering it, but that shouldn't be possible, since I'm registering them on init at priority zero, and my answer key function is being called from the options page on a click, not from a hook at all. (It uses $_GET['display'] and $_GET['exam'] from the plugin .php to call a function to display_admin_exam_key($exam))
So I can display the ordered list of questions, and thats all great, and I can even display the text input box for questions that have a meta identifying them as "short answer" questions, but because I can't reference my exam_item taxonomy, I can't give the text input box the appropriate name, and I can't find the answers that belong to the multiple choice questions.
I'm giving code, but it doesn't really seem to matter HOW I try to reference the taxonomy, it just plain doesn't exist for that block of code.
Here are the hooks for my post types and taxonomies.
add_action( 'init', 'create_wolfe_exam_question_type', 0 );
add_action( 'init', 'create_wolfe_exam_answer_type', 0 );
add_action( 'init', 'create_wolfe_exam_taxonomy', 0 );
add_action( 'init', 'create_wolfe_question_taxonomy', 0 );
Here are the registering bits:
function create_wolfe_exam_taxonomy() {
register_taxonomy(
'wolfe_exam',
'wolfe_exam_question',
array(
'label' => __( 'Exam' ),
'rewrite' => array( 'slug' => 'exam')
)
);
register_taxonomy_for_object_type( 'wolfe_exam', 'wolfe_exam_answer');
}
function create_wolfe_question_taxonomy() {
register_taxonomy(
'wolfe_exam_item',
'wolfe_exam_answer',
array(
'label' => __( 'Exam Item'),
'rewrite' => array( 'slug' => 'examitem')
)
);
register_taxonomy_for_object_type ( 'wolfe_exam_item', 'wolfe_exam_question');
}
function create_wolfe_exam_question_type() {
register_post_type('wolfe_exam_question',
array(
'labels' => array(
'name' => __( 'Exam Questions' ),
'singular_name' => __( 'Exam Question' )
),
'public' => true,
'has_archive' => false
)
);
}
function create_wolfe_exam_answer_type() {
register_post_type('wolfe_exam_answer',
array(
'labels' => array (
'name' => __(' Exam Answers' ),
'singular name' => __( 'Exam Answer' )
),
'public' => true,
'has_archive' => false,
)
);
Here's how I'm calling the display_admin_key:
if (isset($_GET['display']) && (isset($_GET['exam']))) {
switch($_GET['display']) {
case 'admin_test':
wolfe_display_admin_test($_GET['exam']);
break;
case 'user_test':
display_wolfe_exam($_GET['exam']);
break;
case 'score_test':
wolfe_display_score($_GET['exam'], $_GET['examresults']);
break;
}
}
And finally, here's the current bit I'm working on where the taxonomy fails to exist:
function wolfe_display_admin_test($exam) {
echo '<h2>' . $exam . '</h2>';
$args = array(
'post_type' => 'wolfe_exam_question',
'post_status' => 'any',
'wolfe_exam' => $exam
);
$questiondata = new WP_Query($args);
echo '<form action="score_test.php" method="POST">';
echo '<ol type="1">';
while ($questiondata->have_posts()) {
$questiondata->the_post();
echo "<li>" . strip_tags(get_the_content());
$postcustom = get_post_custom();
$questiontype = $postcustom['questiontype'];
$questiontype = $questiontype[0];
$postid = get_the_ID();
if (taxonomy_exists('Exam Item')) echo 'true'; else echo 'false';
$examitem = wp_get_post_terms($postid, 'examitem');
foreach ($examitem as $term) {
echo $term->name;
}
You can see some of my debugging echoes in there. $examitem always comes out WP_ERROR, taxonomy_exists always echoes false, and $term->name always echoes nothing at all.
This is regardless of whether I refer to my custom taxonomy by registered name 'wolfe_exam_item', label 'Exam Item', or slug 'examitem', and no matter which function I try to use to access the terms.
Please help! I've spent hours and hours on this to no avail.