I have created a Custom Post Type called Updates, and setup ACF Field Type “User” so when a new post is created you can select a user. I cannot figure out how to show ONLY the posts that have been selected for the user that is logged in. Please advise.
I don’t see any documentation on the Field Type “User”
You can find the currentl logged in use with this WP code:
After you have the user ID, you can then perform a query on the WP posts via the get_field function.
You can find info on that here:
The meta_name is the field name, and the meta_value is the user ID selected
Thanks Elliot, however I’m having a really difficult time putting this all together, my custom post type is ‘caregiver-updates’ and the advanced custom field that selects the user name is ‘user_id’…
What I need to happen is if the user is logged in it will show all the custom post types of ‘caregiver-updates’ that have the advanced custom field user name ‘user_id’ selected, so somehow check if current user id logged in matches the custom field ‘user_id’ and then pull just those posts, please advise. Thank you so much!!!!
Can you explain where these posts will appear on the website? Is this on a specific page, or perhaps in the footer?
Firstly, please find the appropriate template file to add the code.
Next, research via google / WP how to get the current logged in user ID.
I have already provided a link above to get you started
Next, use this ID to perform a get_posts query and load the posts based on the custom field value.
I have looked over the links you sent as well as researched but I’m having trouble putting it all together, I know how to code into my templates just not having luck getting the code right to do the following…
Page is called Updates and shows custom post types ‘caregiver-updates’, I have it working fine now that it shows ALL the posts… but I want to only show the posts that are assigned to the logged in user by using the Advanced Custom Field User ‘user_id’, it needs to be dynamic incase a new user is created.
See Screen shot of the Advanced Custom Field “User(s) to Share this Update” has the Field name of ‘user_id’
Front end URL to this page is http://caretocontinue.staging.wpengine.com/caregiver-updates/
The fact that your user field is a multi-select is a large factor which will effect the code.
If the field was a sinlgle select, you could perform a simple meta_name / meta_query lookup, but because it is now saved as a serialized array, you will need to allow for this in the meta_query param.
Please look over example 3 on this page:
It should how you can find posts based on a value, which has been selected by a custom field which saves it’s data as an array.
Perhaps you can post your code too.
This is what I have so far and its not pulling any posts…
<?php // args $args = array( 'numberposts' => -1, 'post_type' => 'caregiver-updates', 'meta_key' => 'user_id', 'meta_value' => $current_user->ID ); // get results $the_query = new WP_Query( $args ); // The Loop ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; ?> </ul> <?php endif; ?> <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Can you please confirm that the data is saved as “3” in the DB for the wp_postmeta row where meta_key = ‘user_id’?
I have a feeling that becuase you originally had the field as a multi-select field, the data is saved as a serialized array.
Please change the user field to a single select and resave the posts to update the values in the DB
The topic ‘Field Type "User", only show the posts that the "User" is selected’ 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!
2022 was a big year for Advanced Custom Fields, with more capabilities, a new generation of ACF Blocks, a refreshed UI, and a new home. Our year in review post looks at advancements we’ve made and offers a glimpse of the future.— Advanced Custom Fields (@wp_acf) January 6, 2023
© 2023 Advanced Custom Fields.