Support

Account

Home Forums ACF PRO attach comment to custom post type entry

Solved

attach comment to custom post type entry

    • nicmare

    • August 6, 2014 at 1:24 am

    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?

    • Elliot

    • August 7, 2014 at 3:26 pm

    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.
    http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/

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

    Cheers
    E

    • nicmare

    • August 7, 2014 at 5:48 pm

    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:

    Array
    (
        [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:
    https://www.dropbox.com/s/h4ek9agt4wk97vg/Screenshot%202014-08-07%2009.47.48.png
    so as you can see, the value is still 0,1,2. but it has to be 7,6,5

    • nicmare

    • August 7, 2014 at 5:58 pm

    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!!

    • nicmare

    • August 7, 2014 at 7:14 pm

    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?

    • nicmare

    • August 8, 2014 at 12:55 am

    edited

    • Elliot

    • August 14, 2014 at 1:36 pm

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.