Support

Account

Home Forums Front-end Issues Author page to include Post where in User array custom field

Solved

Author page to include Post where in User array custom field

  • I have Author pages where all of the author’s posts are listed.
    I also have a “Contributors” Users (multiple user) custom field on a post type.
    I am trying to include not only all of the authored posts, but also posts where the user is listed in the array of Contributors.

    I’m seeing something similar here: https://support.advancedcustomfields.com/forums/topic/query-by-user-relationship-field/ for just showing the posts where the user is in the array of custom field users, but I haven’t been able to either:
    1. Get that approach working by itself, or
    2. Figure out how to include that in the page query.

    Official documentation on the Users custom post type seems to be missing.
    How can I add posts where the author is a Contributor to the Author’s list of posts?

  • I just posted a reply in the other topic. What is there should be working

  • Thanks John.

    I ended up using https://github.com/birgire/wp-combine-queries to blend a mostly-normal author posts query with one showing posts where the user is a participant:

    
    function author_include_when_contributor( $query ) {
        if ( !is_admin() && $query->is_main_query() && is_author() ) {
            $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
            $authorArgs = array(
                'post_type'     =>  array('post', 'podcast', 'resources', 'video'),
                'author'        =>  $curauth->ID,
                'orderby'       =>  'post_date',
                'order'         =>  'ASC',
            );
            $contributorArgs = array(
                'post_type' => array('post', 'podcast', 'resources', 'video'),
                'meta_query' => array(
                    array(
                        'key'     => 'participants',
                        'value'   => $curauth->ID,
                        'compare' => 'LIKE'
                    ),
                ),
                'orderby'       =>  'post_date',
                'order'         =>  'ASC',
            );
            $query->set( 'combined_query', [        
                'args'   => [ $authorArgs, $contributorArgs ],
                'union'  => 'UNION',
            ]);
        }
        return $query;
    }
    add_action( 'pre_get_posts', 'author_include_when_contributor' );
    

    I also presented the participants using this on the post:

    
    <?php
    $contributors = get_field('participants'); // array of arrays of user info
    if (!empty($contributors)) {
    	foreach ($contributors as $contributor)
    	{ ?>
    		<div class="col-md-3 col-sm-4 col-xs-2 tac contributor-badge">
    			<a href="<?php echo get_author_posts_url( $contributor ); ?>">
    				<div class="author-image-swap">
    					<?php if ( get_field('primary_picture', 'user_' . $contributor ) ) { ?>
    						<img data-object-fit src="<?php echo get_field('primary_picture', 'user_' . $contributor ); ?>" alt=""/>
    					<?php } elseif (get_the_author_meta('author_profile_picture', $contributor )) { ?>
    						<img data-object-fit src="<?php echo get_the_author_meta('author_profile_picture', $contributor ); ?>" alt="">
    					<?php } else { ?>
    						<img data-object-fit src="<?php echo get_avatar( $contributor, 512 ) ?>" alt=""/>
    					<?php } ?>
    					<?php if ( get_field('secondary_picture', 'user_' . $contributor ) ) { ?>
    						<img data-object-fit src="<?php echo get_field('secondary_picture', 'user_' . $contributor ); ?>" alt="">
    					<?php } ?>
    				</div>
    				<p class="large"><strong><?php echo get_the_author_meta('display_name', $contributor->ID); ?></strong></p>
    				<p><em><?php echo get_field('role', 'user_' . $contributor ); ?></em></p>
    			</a>
    		</div>
    	<?php }
    }
    ?>
    
  • How to list the blog posts where id is either author id or participant id ACF user field in wordpress

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Author page to include Post where in User array custom field’ is closed to new replies.