Home Forums ACF PRO attach comment to custom post type entry


attach comment to custom post type entry

  • i want to attach certain comments to custom post type entries.
    i thought i can achieve this with a relational field.
    like i do when i attach some custom post type to another post types with the “post object” field. but this does not provide “comments” here. any suggestion for this?

  • Hi @nicmare

    Thanks for the question. At the moment, there are no fields which you can use to select comments on a post.

    I believe it will be possible to use a multi-select field, and use the following tutorial to customize the available choices.

    You will need to find some code to query and return the comment ID’s as the select field’s choices


  • nice one elliot! this leads me to the right direction. but i think i found a bug?! i am using ACF 5.0.3.
    this is my function:

    /* auto populate acf field with comment IDs */
    function my_acf_load_field( $field ){
    	$assignedpost = get_field('produktblog');
    	$comments = get_comments(array(
    		'status' => 'approve',
    		'post_id' => $assignedpost->ID
    	if( is_array($comments) )
    		foreach( $comments as $key=>$comment )
    			$field['choices'][ $key ] = $comment->comment_content;
    			$field['default_value'][ $key ] = $comment->comment_ID;
        return $field;
    // v4.0.0 and above
    add_filter('acf/load_field/name=kommentare', 'my_acf_load_field');

    and this is the print_r from $field:

        [ID] => 150
        [key] => field_53e327ee805e6
        [label] => Kommentare
        [name] => kommentare
        [prefix] => 
        [type] => select
        [value] => 
        [menu_order] => 0
        [instructions] => 
        [required] => 0
        [id] => 
        [class] => 
        [conditional_logic] => 0
        [parent] => 6
        [_name] => kommentare
        [_input] => 
        [_valid] => 1
        [choices] => Array
                [0] => kommentar3
                [1] => kommentar2
                [2] => kommentar1
        [default_value] => Array
                [0] => 7
                [1] => 6
                [2] => 5
        [allow_null] => 1
        [multiple] => 1
        [ui] => 0
        [ajax] => 0
        [placeholder] => 
        [disabled] => 0
        [readonly] => 0

    the select field accepts the choices but not the values:
    so as you can see, the value is still 0,1,2. but it has to be 7,6,5

  • alright. my fault!
    this is correct:

    if( is_array($comments) )
    		foreach( $comments as $key=>$comment )
    			$field['choices'][ $comment->comment_ID ] = $comment->comment_content;

    but what is default_value then??
    thank you very much elliot. i love your plugin!!

  • mmmh as soon as i want to save the post with the new values i get this notice:
    PHP Notice: Trying to get property of non-object in /Users/nicb/Data/_htdocs/testnow/wp/wp-includes/post-template.php on line 29
    PHP Notice: Trying to get property of non-object in /Users/nicb/Data/_htdocs/testnow/wp/wp-content/themes/bootstrap/functions.php on line 527

    line 527 if from that function:
    ‘post_id’ => $assignedpost->ID

    but it saves me the post. whats wrong with it?

  • Hi @nicmare

    It looks like the error is coming from your theme, not the WP core or ACF. Have you since found a solution for the issue? If not, I would try changing to the default theme and save the post to see if any other errors appear

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

The topic ‘attach comment to custom post type entry’ is closed to new replies.