Support

Account

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

  • 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(
    		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);