I know this issue has been brought up before, but I’ve read all the posts and am still struggling. Apologies if this should have been obvious.
I am setting up related posts. The related posts are of a particular type (10) and have a repeater field (people) and a sub-field (person_page_id).
I am looking to load only the posts where the sub-field (person_page_id) matches the post_id of the current page. It should be a simple comparison. But nothing I try seems to be working. I’d prefer to use get_posts as it seems to present fewer issues than wp_query.
I have already read all the examples I could find:
So this code works and brings back all the posts in the proper category:
<?php $id = get_the_ID(); ?> <!--get the permalink for the current page -->
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'post',
'category' => 10,
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
But when I add these two lines:
'meta_key' => 'people_%_person_page_id',
'meta_value' => $id,
…I get zero results.
I’ve tried a whole bunch of other variations as well. None of which have worked (obvs or I wouldn’t be asking here).
Every example I see on the web of retrieving values in sub-fields has folks using meta_query to wrap up the values in an array instead of just putting them in meta_key and meta_value. But I’ve tried that too and it hasn’t worked. I’ve also tried variations using wp_query instead of get_posts. Those haven’t made a difference either.
Here’s a version where I moved the arguments into a meta_query array:
<!– fetch albums in which this person is featured –>
<?php $id = get_the_ID(); ?> <!--get the permalink for the current page -->
<?php
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'post',
'category' => 10,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'people_%_person_page_id',
'compare' => '=',
'value' => $id,
)
)
)
);
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Any help would be *much* appreciated. 🙂
–hillel
OK. I found one more example and copied it almost verbatim from the ACF resource guide page here: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I used example #4 which seemed like the closest.
My new code is as follows:
<?php
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'people_%", "meta_key = 'people_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// get the ID of the current page
$currentpersonpageid = get_the_ID();
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'cat' => 10,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'people_%_person_page_id',
'compare' => '=',
'value' => $currentpersonpageid,
)
)
);
echo $currentpersonpageid; //debugging
print_r(array_values($args)); //debugging
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I’ve inspected the current page ID and the target page ID and they are both the same value as they should be.
I’ve inspected the $args that I created and it contains this:
( [0] => -1 [1] => post [2] => 10 [3] => Array ( [relation] => AND [0] => Array ( [key] => people_%_person_page_id [compare] => = [value] => 46 ) ) )
And I’ve run the code without the properties in the meta_query array to try and isolate the problem, and the code works without that bringing back the posts in category ID 10. So all that works.
However, when I add the key piece, the meta_query, I get zero results.
It needs to look at all the posts in category 10, and inspect the ‘person’ repeater sub-field called ‘person_page_id’ and return any posts that match the current posts ID. But nothing.
Any help would be much appreciated.
Hi @hillelc
Thanks for posting your code.
I can see that your ‘posts_where’ filter is missing the word ‘LIKE’ in the str_replace.
Please re-read the 4th example to see where this is needed: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/
BINGO! That did the trick. Not bad, I was only one change away from getting it right.
Thanks SO much for the help. 🙂
The topic ‘Related Posts based on Sub-Field Values’ is closed to new replies.
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.