Home › Forums › Front-end Issues › Query Custom Posts in Page template breaks Custom Fields › Reply To: Query Custom Posts in Page template breaks Custom Fields
I was 5 minutes to early to post the question above as I’ve found the solutions after looking into WordPress Manual.
Seems that if you call another post_type (in my case vacancies) with
new WP_Query, WordPress is opening that (lexical) post_type environment. This means that most functions like ACF’s
the_field() will try to look inside the custom post type “Vacancies” if the custom field “vacancy-section-2-title” exists. Obvious it’s not so i need to “Reset the Query” back to the original one.
The solution is putting this function
wp_reset_query() at the end of the custom posts loop.
<?php $args = array( 'post_type' => 'Vacancies'); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); get_template_part( 'template-parts/vacancies', get_post_format() ); endwhile; wp_reset_query() ?>
Hope it helps other people in the future!
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!
📣 “ACF Chat Fridays”— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.