Support

Account

Home Forums ACF PRO ACF Load Field > Post Object > Render As Checkbox

Solving

ACF Load Field > Post Object > Render As Checkbox

  • Hi there,

    I have a repeater field and would like to ‘attach’ a custom post type to each repeated item via the Post Object field, and I have this set to allow for being able to ‘select multiple items’

    However, I don’t much like the ‘search’ field that renders in the backend.

    Before I get too far into it, is it possible to override this default field rendering via acf_load_field?

  • Just in case it helps anyone…

    After a few minutes of thought, I figured that it was just way easier to dynamically populate a checkbox field with post values instead of superfluous wrangling. My code:

    
    function my_acf_field_checkbox( $field ){
    	
    	global $wpdb;
    	$querystr = "SELECT * FROM '$wpdb->posts' WHERE 'post_type' = 'my-cpt'";
    	$my_cpts = $wpdb->get_results($querystr, OBJECT);
    
    	if($my_cpts){
    		$my_cpt_arr = array();
    		foreach($my_cpts as $my_cpt):
    			$my_cpt_arr[$my_cpt->ID] = $my_cpt->post_title;
    		endforeach;
    	}
    	$field['choices'] = $my_cpt_arr;
    
    	return $field;
    };
    
    add_filter('acf/load_field/name=my_field_name', 'my_acf_field_checkbox');
    

    Hope this helps someone

  • This looks like it’s very similar to something I’m trying to accomplish. Do you have an example of how to implement this (after placing it in your functions.php file and changing the ‘post_type’ that is)?

    E.g. Do you create an empty checkbox field with the name “my_field_name” first? Or do you change “my_field_name” to the name of an existing Post Object field?

    Thanks in advance!

  • Did some additional searching and found this thread on Stack Overflow that showed what I was trying to accomplish. Copying it here, too:

    It will require a little bit of code along with ACF

    Add checkbox field (ex : your_field_name) and add following code in your functions.php file

    function my_acf_load_field( $field )
    {
        global $post;
        $field['choices'] = array();
        wp_reset_query();
        $query = new WP_Query(array(
            'post_type' => 'your-custom-post-type',
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'posts_per_page' => -1,
            ));
        foreach($query->posts as $product_id=>$macthed_product){
                $choices[$macthed_product->ID] = $macthed_product->post_title;
        }
        $field['choices'] = array();
    
        if( is_array($choices) )
        {
            foreach( $choices as $key=>$choice )
            {
                $field['choices'][$key] = $choice;
            }
        }
         wp_reset_query();
        return $field;
    }
    add_filter('acf/load_field/name=your_field_name', 'my_acf_load_field');

    use wp query parameters to filter posts.

    Post id will be the checkbox value with title as label in metabox.

  • Hi guys,

    I’m using this code to load checkboxes to select posts, but I’d like to show the post heirachy.

    It is possible to add divs around the list items that ACF creates for checkboxes? Or add classes to the checkbox list items that this code creates?

    Thanks in advance.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘ACF Load Field > Post Object > Render As Checkbox’ is closed to new replies.