Home Forums Front-end Issues Relationship with Users, CPT and queries


Relationship with Users, CPT and queries

  • Hi everyone! I’ve been using ACF for years without any problem, but now I have to do something too difficult for my abilities and I need some help, I’ll try to explain:

    I have a CPT named ‘Authors’, that has several Custom Fields and a Relationship with Users. I want to link each cpt-Author to a User and basically add Author informations every time a User is shown.

    Now I’m trying to achieve two things:

    – In every (standard) Post page where user info are show, add the custom fileld infos from the Author cpt… is this possible?

    – In every Author page, retrieve all the posts from the assigned user.

    I hope I made myself clear, I dunno if there was an easier way, this is the only way I found, but still I’m stuck and don’t know where to start…


  • Hi! do you think it’s impossible?

  • i think it should maybe possible.
    take a look at this and this

    you need a relationship field at user or/and at author.

    because you should be able to query relationship fields in both directions. you can
    show users at author page, and show author details at user page (at least if users and authors are CPTs)

    but to get it working you need advanced php/ACF/WP knowledge. if you are unable to adapt the example for your needs, you probably need to hire someone to do it for you.

  • Hi! thanks for the answer. I tryied some more but with poor results, it seems wp documentation on users and query is a bit overlooked…

    Where can I find someone to hire some help? Do you have any ideas?
    I’m from Italy and I don’t know anyone capable of doing that…

    Thanks again

  • Hi @andrea_stand

    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(
    			'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);
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Relationship with Users, CPT and queries’ is closed to new replies.