Home › Forums › Add-ons › Repeater Field › Author page with custom post type subfield filter meta query in m
Hi.
I have problem on author.php page. I have custom post type and there is repeater filed “users” with subfield “user_name” (type “User”).
I want to add filter in meta_query on author.php page if that subfield exist and if it is equal to current author page to show all those posts.
Why I need that – For each author I want to have all post which that author was created and also all posts where that user is selected in subfield “user_name”.
author.php code:
<div class="post_article">
<?php
$authorid = get_the_author_meta('ID');
$cat = get_queried_object();
$args = array(
"post_type" => "any",
"post_status" => "publish",
"orderby" => "date",
"order" => 'DESC',
"posts_per_page" => 12,
"author" => $authorid,
'cat' => $cat->term_id
);
$query = new WP_Query($args);
?>
<?php while ($query->have_posts()) : $query->the_post();
echo get_the_author();?>
<article class="post">
<div class="category_thumb">
<a class="post_thumb_list" href="<?php the_permalink() ?>">
<?php the_post_thumbnail('medium');?>
</a>
</div>
<div class="entry_content">
<header class="entry_header">
<?php
$post_title = get_the_title();
?>
<h2><a href="<?php the_permalink() ?>" ><?php echo do_shortcode($post_title); ?></a></h2>
</header>
</div>
</article>
<?php endwhile; ?>
</div>
Thanks.
I solved the problem.
I added this in function.php:
function comapa_posts_where( $where ) {
$where = str_replace("meta_key = 'users_$", "meta_key LIKE 'users_%", $where);
return $where;
}
add_filter('posts_where', 'comapa_posts_where');
And I modified query on author.php:
<?php
global $wpdb;
$author_name = get_the_author();
$authorid = get_the_author_meta('ID');
$cat = get_queried_object();
$args = array(
'post_type' => "any",
'post_status' => "publish",
'orderby' => "date",
'order' => 'DESC',
'posts_per_page' => 12,
'cat' => $cat->term_id,
'meta_query' => array(
array(
'key' => 'users_$_user_name',
'value' => $authorid,
'compare' => '='
)
)
);
$query = new WP_Query($args);
?>
You must be logged in to reply to this topic.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.