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 }
}
?>
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!
ACF’s Repeater field is a versatile solution for repeating content in #WordPress.
— Advanced Custom Fields (@wp_acf) December 29, 2022
What’s your favorite use case for the Repeater field?
What’s the weirdest thing you’ve ever used it for?
Let us know!👇
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.