1

I'm modifying the plugin Wordpress Frontend User for my custom post type(s). What I need to edit is the custom taxonomy terms. In order to do this, I need to list my terms as labels and have checkboxes associated with the labels (in the end associated with the taxonomy when I send a post update).

What I have now is as follows:

$jobsTerms = get_terms('jobtype',array( 'taxonomy' => 'jobtype' )); 
foreach($jobsTerms as $term){
    echo "<label for='term-" . $term->slug . "'>" . $term->name . "</label>";
    echo "<input type='checkbox' name='term" . $term->slug . "' value='" . $term->name . "' />
}

But other than that, I'm lost as to how to check to see if that post has a term selected (if so add checked=checked to the input).

I'm also lost as to how to update this taxonomy when I click submit (which fires a couple custom update_post_meta calls and a wp_update_post call.

Any help?

1

Try this:

$jobsTerms = get_terms('jobtype',array( 'taxonomy' => 'jobtype' )); 
foreach($jobsTerms as $term){
    $checked = (has_term($term->slug, 'jobtype', $post->ID)) ? 'checked="checked"' : '';
    echo "<label for='term-" . $term->slug . "'>" . $term->name . "</label>";
    echo "<input type='checkbox' name='term" . $term->slug . "' value='" . $term->name . "' $checked />";
}

Replace $post_id with whatever you need to get the ID, I'm not sure where exactly this code goes. It may be $post->ID.

  • After changing $post_id to $post->ID (thanks for the suggestion, I didn't notice it was $post_id until I read the whole comment), it now calls them correctly and displays checked for the correct postings. The only thing I'm left with now is finding out how to save changes to them (post_meta?) Anyways, +1 for your answer, I'll be looking in the Gravity Forms + Custom Post Type plugin to see how it's handled. Thanks again! – Josh Allen Aug 26 '11 at 13:09
  • Also, because I can't edit, you should change in the <input> the name='term" to name='term-" to match the consistency for the label. – Josh Allen Aug 26 '11 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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