Home › Forums › General Issues › Issue using post_object field value in WP_Query
Hello! I’m having an issue attempting to retrieve some posts using WP_Query. I’ve spent a few hours trawling through the Internet, but not had a lot of luck.
Just a little background:
I’m attempting to retrieve posts that have an ACF field called show_venue, with a value of the title of the current page. show_venue is a post_object field type.
I’ve arrived at the following:
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'show_venue',
'meta_key_value' => get_the_title(), // "Venue one"
'order' => 'ASC'
);
Then it’s run by WP_Query.
I’m getting three posts back. Two of them are correctly “Venue one”, but a third post with a value of “Venue two” is appearing. Only these three posts have the field value set.
I’d really appreciate any pointers on where I’m going wrong, I just can’t seem to figure it out.
Thanks for your time! 🙂
– Callum Kerr
try:
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'show_venue',
// meta_key_value should be meta_value
'meta_value' => get_the_title(), // "Venue one"
'order' => 'ASC'
);
Hey, thanks for your suggestion. Changing the field name returned no posts at all I’m afraid. Just in case, I tried hardcoding the value instead of get_the_title(), but it was unsuccessful too.
I missed something extremely important in your OP, the field is a post object field
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'show_venue',
'meta_value' => $post->ID, // post objects store the post ID
'order' => 'ASC'
);
EDIT: I made a mistake, your $post->ID suggestion fixed the issue! I forgot to change the post type after I made some changes to the post type holding these details yesterday evening, that’s why it didn’t work.
Thanks a million for your help, I really appreciate it. 🙂
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 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.