Wordpress: Taxonomy assigned to custom post not being shown when post is edited












0















On my Wordpress website I use a Custom Post (called Tracks and can be seen in the attached code). Within this custom Post are taxonomies (called track_category and can also be seen in the attached code). Although each post could have more than one track category, in reality, each track will only have one taxonomy (either 'Hike', 'Walk', 'Mountain bike' or 'Road bike').



I have created a few posts of type Track. Each post has a different taxonomy ('Hike', 'Walk', 'Mountain bike' and 'Road bike')



The user can edit a track. When a track is edited, I want it that the currently assigned taxonomy would be correctly shown in the dropdown list box (the user could then change the taxonomy).



However, my problem is that when the custom post is edited, the assigned taxonomy is NOT displayed. The first taxonomy type (which is Hike) is always shown.



WHY??!



Code is attached. Any ideas would be very welcome.



<?php

// This line is from the post template
echo do_shortcode('[sut_form alreadysubmitted="' . 1 . '" postid="' . $track_ID . '"]');


// Following code is in the plugin
add_shortcode('sut_form', 'sut_form_shortcode');

function sut_form_shortcode($atts, $content = null) {
$params = shortcode_atts(array(
'alreadysubmitted' => false,
'postid' => '',
), $atts );

$sut_postID = $params['postid']; //$sut_postID is now the ID of the custom post being looked at

// Lot of other code
// Lot of other code

$sut_track_name = get_the_title($sut_postID);
$sut_track_sentence = get_field('sentence', $sut_postID);

$terms = get_the_terms($sut_postID, 'track_category');
$sut_track_category = $terms[0]->name; // Custom post can only have one taxonomy so can select the first

echo sut_get_create_track_form($sut_track_name, $sut_track_sentence, $sut_track_category); // Create form
// PROBLEM --> resulting form does NOT show the taxonomy of the post. It always show the first taxonomy ( = Hike)
}


function sut_get_create_track_form($sut_track_name = '', $sut_track_sentence = '', $sut_track_category = ''){
$out .= '<form id="create_track_form" method="post" action="" enctype="multipart/form-data">';
// Other parts of the form
$out .= '<label for="sut_track_category">Track type</label><br/>';
$out .= sut_get_track_categories_dropdown('track_category', $sut_track_category) . '<br/>';
// Other parts of the form
$out .= '<input type="submit" id="sut_submit" name="sut_submit" value="Submit Track For Publication">';
$out .= '</form>';

return $out;
}


function sut_get_track_categories_dropdown($taxonomy, $selected){
return wp_dropdown_categories(array('taxonomy' => $taxonomy, 'name' => 'sut_track_category', 'selected' => $selected, 'hide_empty' => 0, 'echo' => 0));
}


add_action('init', 'sut_plugin_init');

function sut_plugin_init(){
$track_type_labels = array(
'name' => _x('Tracks', 'post type general name'),
'singular_name' => _x('Track', 'post type singular name'),
'add_new' => _x('Add New Track', 'track'),
'add_new_item' => __('Add New Track'),
'edit_item' => __('Edit Track'),
'new_item' => __('Add New Track'),
'all_items' => __('View Tracks'),
'view_item' => __('View Track'),
'search_items' => __('Search Tracks'),
'not_found' => __('No Tracks found'),
'not_found_in_trash' => __('No Tracks found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Tracks'
);


$track_type_args = array(
'labels' => $track_type_labels,
'public' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author')
);


register_post_type('tracks', $track_type_args);

$track_category_labels = array(
'name' => _x( 'Track Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Track', 'taxonomy singular name' ),
'search_items' => __( 'Search Track Categories' ),
'all_items' => __( 'All Track Categories' ),
'parent_item' => __( 'Parent Track Category' ),
'parent_item_colon' => __( 'Parent Track Category:' ),
'edit_item' => __( 'Edit Track Category' ),
'update_item' => __( 'Update Track Category' ),
'add_new_item' => __( 'Add New Track Category' ),
'new_item_name' => __( 'New Track Name' ),
'menu_name' => __( 'Track Categories' ),
);

$track_category_args = array(
'hierarchical' => true,
'labels' => $track_category_labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'track_category' ),
);


register_taxonomy('track_category', array('tracks'), $track_category_args);
$default_track_cats = array('Hike', 'Walk', 'Mountain bike', 'Road bike');
foreach($default_track_cats as $cat){
if(!term_exists($cat, 'track_category')) wp_insert_term($cat, 'track_category');
}
}









share|improve this question



























    0















    On my Wordpress website I use a Custom Post (called Tracks and can be seen in the attached code). Within this custom Post are taxonomies (called track_category and can also be seen in the attached code). Although each post could have more than one track category, in reality, each track will only have one taxonomy (either 'Hike', 'Walk', 'Mountain bike' or 'Road bike').



    I have created a few posts of type Track. Each post has a different taxonomy ('Hike', 'Walk', 'Mountain bike' and 'Road bike')



    The user can edit a track. When a track is edited, I want it that the currently assigned taxonomy would be correctly shown in the dropdown list box (the user could then change the taxonomy).



    However, my problem is that when the custom post is edited, the assigned taxonomy is NOT displayed. The first taxonomy type (which is Hike) is always shown.



    WHY??!



    Code is attached. Any ideas would be very welcome.



    <?php

    // This line is from the post template
    echo do_shortcode('[sut_form alreadysubmitted="' . 1 . '" postid="' . $track_ID . '"]');


    // Following code is in the plugin
    add_shortcode('sut_form', 'sut_form_shortcode');

    function sut_form_shortcode($atts, $content = null) {
    $params = shortcode_atts(array(
    'alreadysubmitted' => false,
    'postid' => '',
    ), $atts );

    $sut_postID = $params['postid']; //$sut_postID is now the ID of the custom post being looked at

    // Lot of other code
    // Lot of other code

    $sut_track_name = get_the_title($sut_postID);
    $sut_track_sentence = get_field('sentence', $sut_postID);

    $terms = get_the_terms($sut_postID, 'track_category');
    $sut_track_category = $terms[0]->name; // Custom post can only have one taxonomy so can select the first

    echo sut_get_create_track_form($sut_track_name, $sut_track_sentence, $sut_track_category); // Create form
    // PROBLEM --> resulting form does NOT show the taxonomy of the post. It always show the first taxonomy ( = Hike)
    }


    function sut_get_create_track_form($sut_track_name = '', $sut_track_sentence = '', $sut_track_category = ''){
    $out .= '<form id="create_track_form" method="post" action="" enctype="multipart/form-data">';
    // Other parts of the form
    $out .= '<label for="sut_track_category">Track type</label><br/>';
    $out .= sut_get_track_categories_dropdown('track_category', $sut_track_category) . '<br/>';
    // Other parts of the form
    $out .= '<input type="submit" id="sut_submit" name="sut_submit" value="Submit Track For Publication">';
    $out .= '</form>';

    return $out;
    }


    function sut_get_track_categories_dropdown($taxonomy, $selected){
    return wp_dropdown_categories(array('taxonomy' => $taxonomy, 'name' => 'sut_track_category', 'selected' => $selected, 'hide_empty' => 0, 'echo' => 0));
    }


    add_action('init', 'sut_plugin_init');

    function sut_plugin_init(){
    $track_type_labels = array(
    'name' => _x('Tracks', 'post type general name'),
    'singular_name' => _x('Track', 'post type singular name'),
    'add_new' => _x('Add New Track', 'track'),
    'add_new_item' => __('Add New Track'),
    'edit_item' => __('Edit Track'),
    'new_item' => __('Add New Track'),
    'all_items' => __('View Tracks'),
    'view_item' => __('View Track'),
    'search_items' => __('Search Tracks'),
    'not_found' => __('No Tracks found'),
    'not_found_in_trash' => __('No Tracks found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => 'Tracks'
    );


    $track_type_args = array(
    'labels' => $track_type_labels,
    'public' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title', 'editor', 'author')
    );


    register_post_type('tracks', $track_type_args);

    $track_category_labels = array(
    'name' => _x( 'Track Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Track', 'taxonomy singular name' ),
    'search_items' => __( 'Search Track Categories' ),
    'all_items' => __( 'All Track Categories' ),
    'parent_item' => __( 'Parent Track Category' ),
    'parent_item_colon' => __( 'Parent Track Category:' ),
    'edit_item' => __( 'Edit Track Category' ),
    'update_item' => __( 'Update Track Category' ),
    'add_new_item' => __( 'Add New Track Category' ),
    'new_item_name' => __( 'New Track Name' ),
    'menu_name' => __( 'Track Categories' ),
    );

    $track_category_args = array(
    'hierarchical' => true,
    'labels' => $track_category_labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'track_category' ),
    );


    register_taxonomy('track_category', array('tracks'), $track_category_args);
    $default_track_cats = array('Hike', 'Walk', 'Mountain bike', 'Road bike');
    foreach($default_track_cats as $cat){
    if(!term_exists($cat, 'track_category')) wp_insert_term($cat, 'track_category');
    }
    }









    share|improve this question

























      0












      0








      0








      On my Wordpress website I use a Custom Post (called Tracks and can be seen in the attached code). Within this custom Post are taxonomies (called track_category and can also be seen in the attached code). Although each post could have more than one track category, in reality, each track will only have one taxonomy (either 'Hike', 'Walk', 'Mountain bike' or 'Road bike').



      I have created a few posts of type Track. Each post has a different taxonomy ('Hike', 'Walk', 'Mountain bike' and 'Road bike')



      The user can edit a track. When a track is edited, I want it that the currently assigned taxonomy would be correctly shown in the dropdown list box (the user could then change the taxonomy).



      However, my problem is that when the custom post is edited, the assigned taxonomy is NOT displayed. The first taxonomy type (which is Hike) is always shown.



      WHY??!



      Code is attached. Any ideas would be very welcome.



      <?php

      // This line is from the post template
      echo do_shortcode('[sut_form alreadysubmitted="' . 1 . '" postid="' . $track_ID . '"]');


      // Following code is in the plugin
      add_shortcode('sut_form', 'sut_form_shortcode');

      function sut_form_shortcode($atts, $content = null) {
      $params = shortcode_atts(array(
      'alreadysubmitted' => false,
      'postid' => '',
      ), $atts );

      $sut_postID = $params['postid']; //$sut_postID is now the ID of the custom post being looked at

      // Lot of other code
      // Lot of other code

      $sut_track_name = get_the_title($sut_postID);
      $sut_track_sentence = get_field('sentence', $sut_postID);

      $terms = get_the_terms($sut_postID, 'track_category');
      $sut_track_category = $terms[0]->name; // Custom post can only have one taxonomy so can select the first

      echo sut_get_create_track_form($sut_track_name, $sut_track_sentence, $sut_track_category); // Create form
      // PROBLEM --> resulting form does NOT show the taxonomy of the post. It always show the first taxonomy ( = Hike)
      }


      function sut_get_create_track_form($sut_track_name = '', $sut_track_sentence = '', $sut_track_category = ''){
      $out .= '<form id="create_track_form" method="post" action="" enctype="multipart/form-data">';
      // Other parts of the form
      $out .= '<label for="sut_track_category">Track type</label><br/>';
      $out .= sut_get_track_categories_dropdown('track_category', $sut_track_category) . '<br/>';
      // Other parts of the form
      $out .= '<input type="submit" id="sut_submit" name="sut_submit" value="Submit Track For Publication">';
      $out .= '</form>';

      return $out;
      }


      function sut_get_track_categories_dropdown($taxonomy, $selected){
      return wp_dropdown_categories(array('taxonomy' => $taxonomy, 'name' => 'sut_track_category', 'selected' => $selected, 'hide_empty' => 0, 'echo' => 0));
      }


      add_action('init', 'sut_plugin_init');

      function sut_plugin_init(){
      $track_type_labels = array(
      'name' => _x('Tracks', 'post type general name'),
      'singular_name' => _x('Track', 'post type singular name'),
      'add_new' => _x('Add New Track', 'track'),
      'add_new_item' => __('Add New Track'),
      'edit_item' => __('Edit Track'),
      'new_item' => __('Add New Track'),
      'all_items' => __('View Tracks'),
      'view_item' => __('View Track'),
      'search_items' => __('Search Tracks'),
      'not_found' => __('No Tracks found'),
      'not_found_in_trash' => __('No Tracks found in Trash'),
      'parent_item_colon' => '',
      'menu_name' => 'Tracks'
      );


      $track_type_args = array(
      'labels' => $track_type_labels,
      'public' => true,
      'query_var' => true,
      'rewrite' => true,
      'capability_type' => 'post',
      'has_archive' => true,
      'hierarchical' => false,
      'menu_position' => null,
      'supports' => array('title', 'editor', 'author')
      );


      register_post_type('tracks', $track_type_args);

      $track_category_labels = array(
      'name' => _x( 'Track Categories', 'taxonomy general name' ),
      'singular_name' => _x( 'Track', 'taxonomy singular name' ),
      'search_items' => __( 'Search Track Categories' ),
      'all_items' => __( 'All Track Categories' ),
      'parent_item' => __( 'Parent Track Category' ),
      'parent_item_colon' => __( 'Parent Track Category:' ),
      'edit_item' => __( 'Edit Track Category' ),
      'update_item' => __( 'Update Track Category' ),
      'add_new_item' => __( 'Add New Track Category' ),
      'new_item_name' => __( 'New Track Name' ),
      'menu_name' => __( 'Track Categories' ),
      );

      $track_category_args = array(
      'hierarchical' => true,
      'labels' => $track_category_labels,
      'show_ui' => true,
      'query_var' => true,
      'rewrite' => array( 'slug' => 'track_category' ),
      );


      register_taxonomy('track_category', array('tracks'), $track_category_args);
      $default_track_cats = array('Hike', 'Walk', 'Mountain bike', 'Road bike');
      foreach($default_track_cats as $cat){
      if(!term_exists($cat, 'track_category')) wp_insert_term($cat, 'track_category');
      }
      }









      share|improve this question














      On my Wordpress website I use a Custom Post (called Tracks and can be seen in the attached code). Within this custom Post are taxonomies (called track_category and can also be seen in the attached code). Although each post could have more than one track category, in reality, each track will only have one taxonomy (either 'Hike', 'Walk', 'Mountain bike' or 'Road bike').



      I have created a few posts of type Track. Each post has a different taxonomy ('Hike', 'Walk', 'Mountain bike' and 'Road bike')



      The user can edit a track. When a track is edited, I want it that the currently assigned taxonomy would be correctly shown in the dropdown list box (the user could then change the taxonomy).



      However, my problem is that when the custom post is edited, the assigned taxonomy is NOT displayed. The first taxonomy type (which is Hike) is always shown.



      WHY??!



      Code is attached. Any ideas would be very welcome.



      <?php

      // This line is from the post template
      echo do_shortcode('[sut_form alreadysubmitted="' . 1 . '" postid="' . $track_ID . '"]');


      // Following code is in the plugin
      add_shortcode('sut_form', 'sut_form_shortcode');

      function sut_form_shortcode($atts, $content = null) {
      $params = shortcode_atts(array(
      'alreadysubmitted' => false,
      'postid' => '',
      ), $atts );

      $sut_postID = $params['postid']; //$sut_postID is now the ID of the custom post being looked at

      // Lot of other code
      // Lot of other code

      $sut_track_name = get_the_title($sut_postID);
      $sut_track_sentence = get_field('sentence', $sut_postID);

      $terms = get_the_terms($sut_postID, 'track_category');
      $sut_track_category = $terms[0]->name; // Custom post can only have one taxonomy so can select the first

      echo sut_get_create_track_form($sut_track_name, $sut_track_sentence, $sut_track_category); // Create form
      // PROBLEM --> resulting form does NOT show the taxonomy of the post. It always show the first taxonomy ( = Hike)
      }


      function sut_get_create_track_form($sut_track_name = '', $sut_track_sentence = '', $sut_track_category = ''){
      $out .= '<form id="create_track_form" method="post" action="" enctype="multipart/form-data">';
      // Other parts of the form
      $out .= '<label for="sut_track_category">Track type</label><br/>';
      $out .= sut_get_track_categories_dropdown('track_category', $sut_track_category) . '<br/>';
      // Other parts of the form
      $out .= '<input type="submit" id="sut_submit" name="sut_submit" value="Submit Track For Publication">';
      $out .= '</form>';

      return $out;
      }


      function sut_get_track_categories_dropdown($taxonomy, $selected){
      return wp_dropdown_categories(array('taxonomy' => $taxonomy, 'name' => 'sut_track_category', 'selected' => $selected, 'hide_empty' => 0, 'echo' => 0));
      }


      add_action('init', 'sut_plugin_init');

      function sut_plugin_init(){
      $track_type_labels = array(
      'name' => _x('Tracks', 'post type general name'),
      'singular_name' => _x('Track', 'post type singular name'),
      'add_new' => _x('Add New Track', 'track'),
      'add_new_item' => __('Add New Track'),
      'edit_item' => __('Edit Track'),
      'new_item' => __('Add New Track'),
      'all_items' => __('View Tracks'),
      'view_item' => __('View Track'),
      'search_items' => __('Search Tracks'),
      'not_found' => __('No Tracks found'),
      'not_found_in_trash' => __('No Tracks found in Trash'),
      'parent_item_colon' => '',
      'menu_name' => 'Tracks'
      );


      $track_type_args = array(
      'labels' => $track_type_labels,
      'public' => true,
      'query_var' => true,
      'rewrite' => true,
      'capability_type' => 'post',
      'has_archive' => true,
      'hierarchical' => false,
      'menu_position' => null,
      'supports' => array('title', 'editor', 'author')
      );


      register_post_type('tracks', $track_type_args);

      $track_category_labels = array(
      'name' => _x( 'Track Categories', 'taxonomy general name' ),
      'singular_name' => _x( 'Track', 'taxonomy singular name' ),
      'search_items' => __( 'Search Track Categories' ),
      'all_items' => __( 'All Track Categories' ),
      'parent_item' => __( 'Parent Track Category' ),
      'parent_item_colon' => __( 'Parent Track Category:' ),
      'edit_item' => __( 'Edit Track Category' ),
      'update_item' => __( 'Update Track Category' ),
      'add_new_item' => __( 'Add New Track Category' ),
      'new_item_name' => __( 'New Track Name' ),
      'menu_name' => __( 'Track Categories' ),
      );

      $track_category_args = array(
      'hierarchical' => true,
      'labels' => $track_category_labels,
      'show_ui' => true,
      'query_var' => true,
      'rewrite' => array( 'slug' => 'track_category' ),
      );


      register_taxonomy('track_category', array('tracks'), $track_category_args);
      $default_track_cats = array('Hike', 'Walk', 'Mountain bike', 'Road bike');
      foreach($default_track_cats as $cat){
      if(!term_exists($cat, 'track_category')) wp_insert_term($cat, 'track_category');
      }
      }






      wordpress






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 22:18









      Alastair GreenAlastair Green

      2417




      2417
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472570%2fwordpress-taxonomy-assigned-to-custom-post-not-being-shown-when-post-is-edited%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472570%2fwordpress-taxonomy-assigned-to-custom-post-not-being-shown-when-post-is-edited%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga