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!
Hi James,
This worked thank you very much!
Just to make sure, this piece of code needs to be placed in the functions.php file? I know it works but I don’t want my code to just hang around on wrong places…