Support

Account

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

  • 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 }
    }
    ?>