WordPress.org

Ready to get started?Download WordPress

Forums

custom taxonomy headaches (5 posts)

  1. ericwolfe
    Member
    Posted 1 year ago #

    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.

  2. ericwolfe
    Member
    Posted 1 year ago #

    To recap, it looks like a wordpress BUG, in which a block of code believes my custom taxonomy does not exist, when in every other piece of code in the same .php, and as evidenced through the basic functionality, it works fine. Even the other custom taxonomy I have works fine in the same block of code. Please, any information?

  3. bcworkz
    Member
    Posted 1 year ago #

    When you click for the answer key, you are reinitializing the entire WP environment, so something different is happening this time around as opposed to the last time around. All your init hooks are priority 0. The order beyond that is not documented, but it appears to be alphabetical. Which should work fine in your case. But if for some reason it were anti-alphabetical, you will be assigning a taxonomy to a non-existing-at-the-time custom post type, causing an unflagged failure.

    I'm not confident at all this is the problem, but it's easy enough to try. Try assigning the taxonomy calls a higher priority number so they execute later than the custom post type calls. I personally don't like low priority numbers, I typically see no advantage to early execution. I'd rather my hooks execute last to avoid other hooks walking over mine. And if my hook gets blocked out by an earlier bad hook, it's fairly easy to find the offender. On that note, disable all plugins to ensure a clean playing field. Even try swapping themes to eliminate that potential conflict.

    Otherwise, keep debugging. See what the global $wp_taxonomies contains after the answer key click. Trace into core code if you have to. I know how easy it is to write it off as a WP bug in frustration, but I think you know how unlikely that really is. Even if it were a bug, you'd have to trace it to it's actual occurrence in order to reliably have others replicate it, so one way or the other, you need to do more debugging. Good luck.

  4. ericwolfe
    Member
    Posted 1 year ago #

    Thank you for the ideas, bcworkz.

    Thinking about your advice about the hook order, I consolidated all of my register functions into one, which registers everything in the appropriate order, to eliminate that as a problem.

    It still didn't work, so I figured that I would simply call the register function at the beginning of my key display function, where the taxonomies failed to exist. And this worked, insofar as the taxonomies now exist at that point.

    However, it killed my WP_Query, and I can now not query even the questions. have_posts() is returning false;

  5. bcworkz
    Member
    Posted 1 year ago #

    Even though my idea did not work out, I'm pleased it got you onto a track that did work.

    On the issue with WP_Query, I don't know if this is the actual problem, but it's something to address anyway. You should not supply your taxonomy as a query argument anymore. This technique was deprecated as of version 3.1. Instead, use the various 'tax_query' arguments to specify your taxonomy. With any luck, this will easily fix your query issue and you will not encounter any more mysteries.

Topic Closed

This topic has been closed to new replies.

About this Topic