Support

Account

Home Forums Front-end Issues Set post object field using php

Solving

Set post object field using php

  • Hello, I am using ACF version 4 and I have some problem in creating a review system for academic proposals.
    In particular, after the user submitted the proposal, creating a new custom post of type “proposal”, the reviewer will reply creating a new custom post of type “review”, using a front-end form.

    In order to link the two posts (proposal and review) I am using a post object in the review containing the proposal title.

    The problem is how do I populate the post object field using php? I was able to pass the variable “proposal title” to the review post, but I was not able to find any solution on web to store this value to the post object field.

    Thank you a lot for your help!

  • Hi @marco82 ,

    I am not sure if I clearly understand your implementation.

    However, I believe you should be able to link a the review to proposal – post object –

    You will need to pass the the post ID to the review form. from the review form, you can then hook into the acf/relationship/query to load that specific post object.

    https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/

    Hopefully this helps.

  • Thank you very much for your reply James. Just a question: using the update field is not the right method, isn’t it?
    I was able to pass post ID in the new_review.php:

    <?php $id_contributo = $_POST['id_contributo']; ?>
    //get proposal idfrom previous page
    <h3>Proposal reviewed: <?php echo $id_contributo; ?></h3>//display original proposal title
    <?php update_field('proposal_reviewed', $id_contributo);?> //update post object field content
    <?php echo get_field('id_contributo');?> //echo the value
    
    //form used to create the new review
    <?php acf_form(array(
    'post_id'		=> 'new_review',
    'submit_value'	=> 'Create',
    ));?>

    and in function.php:

    function my_pre_save_review( $post_id ) {
    
    	// bail early if not a new review
    	if( $post_id != 'new_review' ) {
    		
    		return $post_id;
    			
    	}
    	
    	
    	// Create a new post
    	$review = array(
    		'post_status'	=> 'publish',
    		'post_type'		=> 'reviews',
    	);	
    	
    	$post_id = wp_insert_post( $review ); 
    
    	
    	// return the new ID
    	return $post_id;
    
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_review' );

    Echoing the value in the front-end works, but if I open the review from admin panel the object field is not updated.

  • Ok, I am following your advice. Can you point me in the right direction on how to pass the proposal id in the post object filter code? I am trying to figure out where to look..
    Thank a lot!

    function assign_proposal_to_review( $args, $field, $post_id ) {
    	
        // only show children of the current post being edited
        $args['page_id'] = '???';
    	
    	
    	// return
        return $args;
        
    }
    
    add_filter('acf/fields/post_object/query/name=proposal_reviewed', 'assign_proposal_to_review', 10, 3);
  • Hi @marco82 ,

    You could use the post_parent in the query as

     // only show children of the current post being edited
        $args['post_parent'] = $post_id;
    	
    	// return
        return $args;

    Let me know how it goes.

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

The topic ‘Set post object field using php’ is closed to new replies.