Home › Forums › General Issues › Can\'t Filter wp-query by a relationship ACF field
I have tried everything, but I can’t seem to be able to filter a search by this relationship field. What I need to do:
I have a custom post type called programs and another one called Testimonials
So I have added in each testimonial a relationship field with all programs listed, so you select all the pages you want that testimonial to show. Ok, so, inside a program page, I need to use a query that shows only the testimonials that have programs selected. and I also need to order them by this relationship field, otherwise if I do it randomly, it might get a testimonial that has no programs linked to it, so it would fall out of the loop, showing a bunch of unrelated testimonials. Ok, once I am in the loop of testimonials with programs linked to it, I need to check if the programs connected to those testimonials are the same as in the current program page we are in. Oh, and also on top of that, I can show only one testimonial at a time….
This is what I tried that didn’t work (some of them, I tried many)
$args = array(
'numberposts' => -1,
'post_type' => 'testimonials',
/*'meta_query' => array(
array(
'key' => 'program_page',
'value' => '',
'compare' => '!='
)
) */
);
$query1 = new WP_Query( $args );
if ( $query1->have_posts() ) {
while ( $query1->have_posts() ) {
$query1->the_post();
$getcontacts = get_field('program_page');
foreach( $getcontacts as $getcontact ):
$getcontactid = $getcontact->ID;
if ($getcontactid == $page_id) { ?>
<div class="span12" data-aos="fade-up">
<blockquote>
<span>“</span><?php the_field('testimonial_text'); ?><span>”</span>
<cite><?php the_title(); ?>
<small><?php the_field('program'); ?></small>
</cite>
</blockquote>
</div>
<?php break; ?>
<?php } else {
wp_reset_postdata();
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 1,
);
$query2 = new WP_Query( $args );
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post();?>
<div class="span12" data-aos="fade-up">
<blockquote>
<span>“</span><?php the_field('testimonial_text'); ?><span>”</span>
<cite><?php the_title(); ?>
<small><?php the_field('program'); ?></small>
</cite>
</blockquote>
</div>
<?php }
}
}
endforeach; ?>
<?php }
} else { ?>
<?php /*
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="span12" data-aos="fade-up">
<blockquote>
<span>“</span><?php the_field('testimonial_text'); ?><span>”</span>
<cite><?php the_title(); ?>
<small><?php the_field('program'); ?></small>
</cite>
</blockquote>
</div>
<?php }
}*/ ?>
No test to show.
<?php }
?>
<?php wp_reset_postdata(); ?>
any idea of what I could be doing wrong???
You can’t do the query that you’re trying to do. A relationship field holds an array of post IDs and is stored as serialized data in the DB.
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
https://www.advancedcustomfields.com/resources/querying-relationship-fields/
You will not be able to order your posts by this field.
What you need to do is create a bidirectional relationship so that you can get the testimonials that need to be shown when you are showing the program rather than trying to query the testimonials that have the program.
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 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.