Home › Forums › Backend Issues (wp-admin) › Query Posts by ACF Value in Functions.php
Hi there,
I’ve been trying to work this out for some time now with no luck. Hopefully somebody can point me in the right direction.
I’m in the process of putting together a WooCommerce site where people can book tours. Each tour is a bookable product (with WooCommerce Bookings). I’ve enabled a custom post type called guides which gives us the ability to add individual tour guides.
I’ve also created an ACF which has a field type of Post Object. This lists all of the posts in my gotread-guides post type and allows us to select a guide from a dropdown within the product page.
What I’d like to do is echo out that guide’s photo, name and bio on the WooCommerce single product page. I can’t see to figure out how I can echo out the post ID based on the value of the ACF field.
This is what I’ve got so far:
//Add a guide
add_action( 'woocommerce_product_thumbnails', 'gotread_guide', 110 );
function gotread_guide() {
echo '<div class="row clear">';
echo '<div class="col-4">';
echo '</div>';
echo '<div class="col-8">';
$post_id = 176;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo $queried_post->post_content;
echo '</div>';
echo '</div>';
}
Which works, but only echoes out post 176. What I need to do is get that ID to change whenever the guide in the dropdown on the product page is changed.
Any help would be greatly appreciated!
Thank you.
If you want an image or other content to change when a select field is changed then this is something that you’d need to do using JavaScript and possibly AJAX, not something that you can do using just PHP.
Hi John,
Thanks for getting back to me.
Fortunately, I was able to echo out the post content based on the ACF drop-down selection. I had to make a couple of alterations for this to work though, so it’s not as automated as I was hoping for.
So basically I changed the custom field type from Post Object to Select and just inserted a list of post IDs along with their respective guide names.
I then altered the PHP in functions to this:
//Add a guide
add_action( 'woocommerce_product_thumbnails', 'gotread_guide', 110 );
function gotread_guide() {
echo '<div class="row clear">';
echo '<div class="col-4">';
echo '</div>';
echo '<div class="col-8">';
$guideid = get_field('choose_your_guide', false, false);
$post_id = $guideid;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo $queried_post->post_content;
echo '</div>';
echo '</div>';
}
A new variable of $guideid
was added which was equal to get_field('choose_your_guide', false, false);
I realised it wasn’t working before because it was echoing the ID as a string rather than a number so adding false to the $format_value
parameter stopped this. Which is pulling out both the title and the post content. Which is great, but you’re right I am having trouble with the images.
I want to echo out the featured image. How would you recommend I go about this using JavaScript?
Just a quick one to say I’ve figured it out, at long last.
All I had to add was the following line:
echo get_the_post_thumbnail( $queried_post->ID, 'thumbnail' );
DONE!
Thanks for your help.
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.