This is the codes i wrote for front end submission form that appears in all the companies single.php page for the clients
<?php
while(have_posts()):the_post();
acf_form(array(
‘post_id’ => ‘new_post’,
‘post_title’ => true,
‘post_content’ => false,
‘field_groups’ => array(648),
‘form’ => true,
‘new_post’ => array(
‘post_status’ => ‘publish’,
‘post_type’=>’message’,),
‘submit_value’ => ‘Submit’,
‘updated_message’ => ‘Thanks, you have successfully submitted your posts ‘
));endwhile ?>
//This is the code for querrying all messages sent by clients for a specific company’s dashboard page(dasboard.php) the company admin login.
<?php
$user = get_current_user_id();
$args = array(
‘post_type’=>’message’,
‘author’=>$user,
);
$query = new WP_Query($args);?>
<?php while ($query ->have_posts() ) : $query ->the_post(); ?>
<p><?php the_field(‘name’);?></p>
<p><?php the_field(‘subject’);?></p>
<?php endwhile; wp_reset_query();// end of the loop. ?>
// This is the codes for updating client of the newly created messages with the companies ID. (functions.php)
function my_acf_save_post_change_author( $post_id ) {
// Get the company’s user ID (depends on how you set it)
$company_id = 2;
// Only do this if posted from front end
if( !is_admin() ){
// Get the post object
$the_post = get_post($post_id);
// Check the post type
if( $the_post->post_type == ‘message’ ){
// Set the data you want to update
$my_post = array(
‘ID’ => $post_id,
‘post_author’ => $company_id,
);
// Remove action to avoid infinite loop
remove_action(‘acf/save_post’, ‘my_acf_save_post_change_author’, 20);
// Update the post into the database
wp_update_post( $my_post );
// Add the action back
add_action(‘acf/save_post’, ‘my_acf_save_post_change_author’, 20);
}
}
}
// run after ACF saves the $_POST[‘acf’] data
add_action(‘acf/save_post’, ‘my_acf_save_post_change_author’, 20);
Thanks a lot.
But the code is still not working.
There are very many companies who are signing up . How can i get all their ID’s dynamically without using
// Get the company’s user ID (depends on how you set it)
$company_id = 2;
Its like the post author of the newly created messages is not updating with the companies ID.
The query $args = array( ‘post_type’=>’message’,’author’=>$user );
Is returning nothing
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.