WordPress.org

Support

Support » Plugins and Hacks » Magic Fields 2 » [Resolved] populate checkbox_list with related type or term

[Resolved] populate checkbox_list with related type or term

  • Hello,

    Thanks for the great plugin.
    I am looking for a way to populate a checkbox_list with a list of post of a certain type (like the custom field “related type” or “term” do).

    Basically, the related type custom field works and give me a nice dropdown list of all my posts of a certain type! great but what I would like is to be able to select a few posts in the list (not possible with a dropdown menu), even better would be to have a nice checkbox list with all my post of a certain type so one can have a nice over view of all post and check some!

    So I guess the best would be to create my own custom field but… it’s hard…. I tryed to play around with related_type_field.php and checkbox_list_field.php but no big success…

    Anybody?

    http://wordpress.org/extend/plugins/magic-fields-2/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try this:

    <?php
    // This code should be in a file named "alt_related_type_field" in the folder "field_types" of Magic-Fields-2
    // You also need to provide icon_color.png, icon_gray.png and preview.png which you can temporarily borrow
    // from its siblings for now.
    
    // initialisation
    global $mf_domain;
    
    // class with static properties encapsulating functions for the field type
    
    class alt_related_type_field extends mf_custom_fields {
    
      public $allow_multiple = TRUE;
      public $has_properties = TRUE;
    
      public function _update_description(){
        global $mf_domain;
        $this->description = __("Checkbox list that lets a user select ONE or MORE related posts of a given post type.",$mf_domain);
      }
    
      public function _options(){
        global $mf_domain;
    
        $posttypes = $this->mf_get_post_types();
        $select = array();
        foreach($posttypes as $k => $v){
          $select[$k] = $v->label;
        }
    
        $data = array(
          'option'  => array(
            'post_type'  => array(
              'type'        =>  'select',
              'id'          =>  'post_type',
              'label'       =>  __('Related Type Panel (Post type)',$mf_domain),
              'name'        =>  'mf_field[option][post_type]',
              'default'     =>  '',
              'options'     => $select,
              'add_empty'   => false,
              'description' =>  '',
              'value'       =>  '',
              'div_class'   => '',
              'class'       => ''
            ),
            'field_order'  => array(
              'type'        =>  'select',
              'id'          =>  'field_order',
              'label'       =>  __('Field for order of Related type',$mf_domain),
              'name'        =>  'mf_field[option][field_order]',
              'default'     =>  '',
              'options'     => array('id' => 'ID','title' =>'Title'),
              'add_empty'   => false,
              'description' =>  '',
              'value'       =>  '',
              'div_class'   => '',
              'class'       => ''
            ),
            'order'  => array(
              'type'        =>  'select',
              'id'          =>  'order',
              'label'       =>  __('Order of Related type',$mf_domain),
              'name'        =>  'mf_field[option][order]',
              'default'     =>  '',
              'options'     => array('asc' => 'ASC','desc' =>'DESC'),
              'add_empty'   => false,
              'description' =>  '',
              'value'       =>  '',
              'div_class'   => '',
              'class'       => ''
            )
          )
        );
    
        return $data;
      }
    
      public function display_field($field, $group_index = 1, $field_index = 1){
        $output = '';
        $check_post_id = null;
        if( !empty($_REQUEST['post'])) {
          $check_post_id = $_REQUEST['post'];
        }
    
        $values = array();
        if($check_post_id){
          $values = ($field['input_value']) ? (is_serialized($field['input_value']))? unserialize($field['input_value']): (array)$field['input_value'] : array() ;
        }
    
        $type        = $field['options']['post_type'];
        $order       = $field['options']['order'];
        $field_order = $field['options']['field_order'];
    
        $options = get_posts( sprintf("post_type=%s&numberposts=-1&order=%s&orderby=%s&suppress_filters=0",$type,$order,$field_order) );
    	$option_ids = [];
    	foreach($options as $option) {
          $option_is[] = $option->ID;
    	}
        $output = '<div class="mf-checkbox-list-box" >';
    
          foreach($values as &$val){
            $val = trim($val);
          }
    
        foreach($options as $option){
          $check = in_array($option->ID, $values) ? 'checked="checked"' : '';
    
          $output .= sprintf('<label for="%s_%s" class="selectit mf-checkbox-list">',$field['input_id'],$option->ID);
          $output .= sprintf('<input tabindex="3" class="checkbox_list_mf" id="%s_%s" name="%s[]" value="%s" type="checkbox" %s %s />',
    					$field['input_id'],$option->ID,$field['input_name'],$option->ID,$check,$field['input_validate']);
          $output .= esc_attr($option->post_title);
          $output .= '</label>';
        }
    
        $output .= '</div>';
    	#error_log( "##### alt_related_type_field::display_field() returns $output\n" );
        return $output;
      }
    
    }

    Thanks,

    It seems thit works fine! I’ll update you if I find any bugs!

    I’m planning to have a page with all custom_post_type listed. The user can check which post_type to display in front page.

    Nice

    Thanks,

    It seems thit works fine! I’ll update you if I find any bugs!

    I’m planning to have a page with all custom_post_type listed. The user can check which post_type to display in front page.

    Nice

    Hi Magenta Cuda,

    I have copied the code but it says that there’s an error on line 94:

    Parse error: syntax error, unexpected ‘[‘

    $option_ids = []; This is the code on line 94.

    Can you help me?

    Thanks!

    You are probably running an older version of PHP. The short array syntax which replaces array() with [] requires at least PHP 5.4. Is it possible for you to upgrade your PHP server? Otherwise you can just replace [] with array(). Incidentally, I have made this code available in a plugin. Of course the plugin also requires PHP 5.4 so it can only help you if you upgrade your PHP server.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] populate checkbox_list with related type or term’ is closed to new replies.
Skip to toolbar