Tagged: , , ,

This topic contains 8 replies, has 3 voices, and was last updated by  Steve 4 years, 4 months ago.

  • Author
    Posts
  • #1777

    mjenn123
    Participant

    I’ve created a custom post type and a custom taxonomy. I’m able to see the list of terms as a radio field on the custom post type using Piklist, but when I select one and save it, it does not seem to actually associate the cpt with the term. I’ve looked over my code dozens of times and tried several variations. I’m hoping I’m missing something obvious.

    Any help would be welcome.

    Here’s my plugin code (note that it’s not a theme).

    Taxonomy definition:

    add_filter('piklist_taxonomies', function( $taxonomies ){
    
      $taxonomies[] = array(
        'post_type' => 'sms_band'
        ,'name' => 'sms_shade'
        ,'show_admin_column' => true
        ,'hide_meta_box' => false
        ,'configuration' => array(
          'hierarchical' => true
          ,'labels' => piklist('taxonomy_labels', 'Shade')
          ,'show_ui' => true
          ,'query_var' => false
        )
      );
      return $taxonomies;
    
    });

    Code in use to add two default terms

    // Add 'Dark' and 'Light' default terms
    add_action('admin_init', function(){
    
      $taxonomy = "sms_shade";
    
      $term = "Dark Background [Light Text]"; 
      $slug = "dark"; 
      if ( !term_exists( $term, $taxonomy ) ){
        wp_insert_term( $term, $taxonomy, array( 'slug' => $slug ) );
      }
      $term = "Light Background [Dark Text]"; 
      $slug = "light"; 
      if ( !term_exists( $term, $taxonomy ) ){
        wp_insert_term( $term, $taxonomy, array( 'slug' => $slug ) );
      }
    
    });

    /parts/meta-boxes/band_shade.php

    piklist('field', array(
      'type' => 'radio'
      ,'field' => 'band_shade'
      ,'scope' => 'taxonomy'
      ,'label' => 'Shades'
      ,'choices' => piklist(
      get_terms('sms_shade', array(
        'hide_empty' => false
      ))
      ,array(
        'term_id'
        ,'name'
      ))
    ));
  • #1778

    mjenn123
    Participant

    Is it possible that I’m defining an incorrect scope? I can’t find what the options for that are other than ‘post_meta’ as explained here: http://piklist.com/user-guide/docs/fields/field-parameters/scope/

    Is there further documentation somewhere that lists all the scope options available?

  • #1779

    mjenn123
    Participant

    The scope must not be the issue. In this tutorial it uses ‘scope’ => ‘taxonomy’. I must be doing something else wrong.

  • #1783

    Steve
    Keymaster

    @mjenn123– This is working for me. The only difference is that I associated band_shade with Post, since I don’t have the CPT sms_band registered.

    Also, do you have the necessary comment block at the top of: /parts/meta-boxes/band_shade.php ? Here is the doc on the comment block >

  • #1784

    mjenn123
    Participant

    Here’s the comment block I’m using.

    
    /*
    Title: Band Options
    Post Type: sms_band
    */
    

    I must be doing something wrong. Probably some tiny little argument somewhere I’m missing.

    When you say you “associated band_shade with post, did you do it like this:

    
    /*
    Title: Band Options
    Post Type: post
    */
    
  • #1791

    Steve
    Keymaster

    @mjenn123– If you want, please zip up your code and email to [email protected]

  • #3780

    Quang Anh
    Participant

    Hi Steve,

    I have the same problems with taxonomy.

    functions.php

    add_filter('piklist_taxonomies', 'restaurant_cuisine');
    function restaurant_cuisine($taxonomies) {
      $taxonomies[] = array(
        'post_type' => 'restaurant'
        ,'name' => 'cuisine'
        ,'show_admin_column' => true
        ,'configuration' => array(
          'hierarchical' => true
          ,'labels' => piklist('taxonomy_labels', 'Cuisines')
          ,'hide_meta_box' => true
          ,'show_ui' => true
          ,'query_var' => true
          ,'rewrite' => array( 
            'slug' => 'cuisine' 
          )
        )
      );
      return $taxonomies;
    }

    restaurant_metabox.php

    piklist('field', array(
      'type' => 'select'
      ,'scope' => 'taxonomy'
      ,'field' => 'restaurant_cuisine'
      ,'label' => 'Cuisine'
      ,'description' => 'Choose the Cuisine taxonomy.'
      ,'attributes' => array(
          'multiple' => 'multiple'
        )
      ,'choices' => $choices
      
    ));

    And I find a trick in that topics.
    https://piklist.com/support/topic/warning-illegal-offset-type-in-isset-or-empty/

    When I removing scope it work. But the problem here is it do not effect the list taxonomies you choose. It store data in a different way not the ways you choose taxomony for your post and using with get_the_terms() function to get data. Can you help me find a solution.

    Thanks

  • #3781

    Quang Anh
    Participant

    Hi Steve,

    I find your solution here: https://piklist.com/support/topic/my-field-is-not-saved/

    Thanks./.

  • #3782

    Steve
    Keymaster

    Great. Closing ticket

The topic ‘Taxonomy field not saving on CPT’ is closed to new replies.