Home › Forums › Front-end Issues › Relationship with Users, CPT and queries › Reply To: Relationship with Users, CPT and queries
This is absolutely possible to do and it’s really just a basic relationship querying and reverse querying.
These two different queries should get you going! If you need further extensive help you can contact me directly at [email protected] for help through our web agency.
//This should be in the loop
//Fetch an author post based on the current post authors (users) ID
$author_ID = get_the_author_meta('ID');
$args = array(
'post_type' => 'authors',
'meta_query' => array(
array(
'key' => 'userfieldname',
'value' => $author_ID,
'compare' => 'LIKE'
)
)
);
$author_query = new WP_Query($args);
//fetch all posts for the user. I'm not 100% sure the user field type returns an user object but I think so.
//It's also possible that $author is an array with user objects inside. if so change $author->ID to $author[0]->ID.
$author = get_field('userfieldname');
$args = array(
'post_type' => 'authors',
'author' => $author->ID
);
$usersposts_query = new WP_Query($args);
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.