Home › Forums › Front-end Issues › Querying all posts send by non-login user to a specific author
I made acf front end submission form on single.php page for a lists of companies who sign up on my site.
The form is for non login clients to send message for each company via single.php.
I want each company to see all posts(messages) sent by their clients , after logging in
This is code i wrote.
<?php $user = get_current_user_id();
$args = array(‘post_type’=>’companies’,’author’=>$user, );
$query = new WP_Query($args);?>
<?php while ($query->have_posts() ) :
$query->the_post();?>
<p><?php echo the_field(‘name’)?></p>
<p><?php the_field(‘Message’);?></p>
<?php endwhile; wp_reset_query();// end of the loop. ?>
BUt IT IS NOT WORKING
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 🙂
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
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);
As I said, it depends on how you set it. For example, if you have a post for a company, you can use the user field to set the user associated with that post. Then I believe you can get the company’s user ID like this:
global $post;
$company_id = get_field('company_user_field_name', $post->ID, false);
If that doesn’t work, you can always add hidden input by using the html_after_fields
option like this:
'html_after_fields' => '<input type="hidden" name="companyid" value="'. get_the_ID() .'">',
And then get the company ID like this:
$company_id = get_field('company_user_field_name', $_POST['companyid'], false);
Also, could you please try to set the $company_id
manually to your account’s ID and check if the query shows the messages for you?
Thanks 🙂
The topic ‘Querying all posts send by non-login user to a specific author’ is closed to new replies.
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.