Home › Forums › Front-end Issues › Querying all posts send by non-login user to a specific author › Reply To: Querying all posts send by non-login user to a specific author
I’m not sure how your current setup is, so I’ll assume that the messages are stored as a custom post type called “message”. With this setup, you need to update the post author of the newly created messages with the companies ID.
It should be something like this:
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);
After that you can query it like this:
$args = array( 'post_type'=>'message','author'=>$user );
I hope this helps 🙂
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.