Home › Forums › General Issues › Querying a Post Object relation field inside a repeater › Reply To: Querying a Post Object relation field inside a repeater
Thanks, James. That helped get me on my way. What threw me off were the percentage marks and incorrectly assuming that the related code only applied to dates, or was something I couldn’t modify to my purpose, to my particular variables.
I also was wrong in how I told the query to pull up posts matching a value on a checkbox. Anyway, for the interests, here’s what I ended up using. This query looks for posts of another content type that 1) are linked through post object to the current page’s content type, and 2) either are (in this case) or aren’t checked in a checkbox indicating department head status.
<?php
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'person_department_%", "meta_key LIKE 'person_department_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// checks if there's a department head
// if so, displays that person
// args
$args_faculty_head = array(
'posts_per_page' => -1,
'post_type' => 'people',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'person_department_%_person_department_select', // this should be the first sub-field
'value' => get_the_ID(),
'compare' => '='
),
array(
'key' => 'person_department_%_person_department_head', // this should be the second sub-field
'value' => '"Yes"',
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query( $args_faculty_head );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
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.