I have a custom post type “testimonials” that has a few fields added to it via ACF Post Object. I call them related fields – they are not set up as a taxonomy, but they act like taxonomy in this situation. They are actually other custom post types. They are country and continent.
These related fields are set in ACF to appear in posts that are testimonials, and that is working great. Example:
Testimonial title: Ben’s Great Trip
Related Country: Kenya
Related Continent: Africa
See screenshot: https://snipboard.io/EMUZ3F.jpg
I am building a custom block, for which everything is already setup and in place, but I’m stuck correctly writing query. The block should show the latest testimonial posts that have a specific related field, newest first, with pagination optional.
So my block would have
– a field to choose which related field (a Post Object unless you have a better idea)
– a field to choose how many testimonial posts
– a field to optionally set a limit (only show the latest 5, etc)
So typical use would be to add this block to the Kenya page, to display the testimonials with Kenya in their Country field.
I’ve gotten stuck writing the query, but am familiar with most of the parameters. I’ve spent a lot of time duplicating other blocks on this site that do similar things, but haven’t gotten it right yet.
The code below works, except my custom field does not show up.
$args = array( 'post_type' => 'testimonial', 'post_status' => 'publish', 'posts_per_page' => 5, 'orderby' => 'title', 'order' => 'ASC', ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); $featured_img = wp_get_attachment_image_src( $post->ID ); $countryTestimonial = get_field('testimonial_country'); ?> <h3><?php echo $countryTestimonial; ?></h3> <?php print the_title(); the_excerpt(); endwhile; wp_reset_postdata();
Sometimes there is more than one country added to a testimonial post, as i underlined in that screenshot above, so that’s probably part of the problem – the related fields are arrays sometimes having one but maybe more countries added.
Do i need to add the related custom fields in the
$ars section instead of where i did in the code above? Do i need to somehow search the array to find Kenya (in this case)?
I cannot help you with blocks.
This issue is that the the relationship in the testimony post only stores post ID values of the country and continent posts. What you are trying to accomplish cannot be done using wp_query, or at least not a single query, unless you already know the ID values of the country continent post, if you are building this on a specific country or contentment page then this could be done because you have the correct post ID. However it cannot be done using blocks because the relationship field is not stored in post meta when using the new block editor, the fields are stored in “post_content”
Sorry, I’m not sure if this is any help because I don’t know how these relationships are set up for the testimonial. Are they in blocks or not?
This is how you would do this if you were using acf fields not in blocks.
The relationships are setup like this:
– Related Fields Country, Continent
— They are both ACF Post Objects, set to allow multiple, and to display on post types of Testimonial.
So these are not set to show in blocks, but blocks are being used to display Testimonials as I am trying to and already in other custom blocks showing testimonials in some other way.
I’m not sure if that answers your question, but i will also read about the link you sent of querying relationship fields.
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!