Support

Account

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!