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!
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.