Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been using a chunk of code as a plugin that was suggested in the answer to this question. Please check the code provided below instead of the one in the question, as I may have modified it a bit. The plugin works fine for the normal post type but has an issue when used with custom-post-type. My custom post type is called "research".

To be explicit, the code is not able to save the tag/term assigned with checkboxes, when used in the single-template of custom post type "research". However it does save the tags assigned with checkboxes, when used in the single-template of the normal-post-type. I would really appreciate the correction in the code please.

FRONTEND TAGGING PLUGIN CODE :

add_action('init', 'frontend_term_assignment');
add_action('frontend_term_assignment_form', 'frontend_term_assignment_form');


function frontend_term_assignment() {
  if (
    ! isset($_POST['term_assignment_post_id']) ||
    ! intval($_POST['term_assignment_post_id']) ||
    ! current_user_can('manage_options', $_POST['term_assignment_post_id']) ||
    ! isset( $_POST['term_assignment_nonce'] ) ||
    ! wp_verify_nonce($_POST['term_assignment_nonce'], 'frontend_term_assignment')
  ) return;
  $post = get_post( $_POST['term_assignment_post_id'] );
  $taxonomy = null;
  if ( $post->post_type == 'post' ) {
    $taxonomy = 'post_tag';
  } elseif ( $post->post_type == 'research' )  {
    $taxonomy = 'research_sections';
  }
  if ( empty($taxonomy) || ! isset($_POST[$taxonomy]) ) return;
  $terms = $_POST[$taxonomy];
  wp_set_post_terms( $post->ID, $terms, $taxonomy, false);
}

function frontend_term_assignment_form() {
  $post = get_queried_object();
  if ( empty($post) || ! isset($post->ID) || ! current_user_can('manage_options', $post->ID) )
  $taxonomy = null;
  if ( $post->post_type == 'post' ) {
    $taxonomy = 'post_tag';
  } elseif ( $post->post_type == 'research' )  {
    $taxonomy = 'research_sections';
  }
  if ( empty($taxonomy) ) return;
  $tax_obj = get_taxonomy($taxonomy);
  $args = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false );
  $terms = get_terms($taxonomy, $args);
  $post_terms = (array)wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "ids") );
  if ( ! empty($terms) ) {
    echo '<form method="post" id="frontend_term_assignment_form">';
    echo '<input type="hidden" value="' . wp_create_nonce('frontend_term_assignment') . '" name="term_assignment_nonce">';
    echo '<input type="hidden" value="' . $post->ID . '" name="term_assignment_post_id">';
    echo '<div class="' . $taxonomy . '_term_list"><ul>';
    foreach ($terms as $term ) {
      $checked = in_array($term->term_id, $post_terms) ? checked(1, 1, 0) : '';
      printf('<li><label><input type="checkbox" value="%s" name="%s[]"%s> ' . $term->name . '</label></li>', $term->slug, $taxonomy, $checked);
    }
    echo '</ul><input type="submit" id="tag-sub" value="' . esc_attr( sprintf(__('Save %s'), $tax_obj->label) ) . '" />';
    echo '</div></form>';
  }
}

ADDED NOTES : Below given is a the code that is used in the single.php templates which displays the form. Tags if assigned from the wp-backend works fine and is saved fine, the trouble is just saving tags in the frontend.

<?php do_action('frontend_term_assignment_form'); ?> 
share|improve this question
    
What is this: add_action('frontend_term_assignment_form', 'frontend_term_assignment_form');? –  s_ha_dum Dec 6 '13 at 14:57
    
@s_ha_dum Add_action enables the use of do_action, which I use on a single.php in order to display a form on the front-end which lets me assign tags. Hope that was clear. Here is the complete code that is added in my single.php. <?php do_action('frontend_term_assignment_form'); ?>. –  gurung Dec 6 '13 at 16:55
add comment

1 Answer 1

wp_set_post_terms only works with the post post type, use wp_set_object_terms for all post types.

share|improve this answer
    
No, the issue is still unsolved. On line 22, I changed the wp_set_post_terms to wp_set_object_terms , also on line 38 I changed the wp_get_post_terms to wp_get_object_terms. This still allows me to save tags in normal-post-type but not in custom-post-type. Your input is greatly appreciated. –  gurung Dec 7 '13 at 10:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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