Home Forums General Issues Problem to assemble query with two posts_types ACF with relational fields


Problem to assemble query with two posts_types ACF with relational fields

  • Hello, how are you? I’m trying to make a query between two post types to build a page that shows 05 news. This query will get the name of the news, with the post type “post” and the name of the news columnist along with his photo with post type “columnist”. I’m using relational field in ACF. I’m used to using the ACF, however, this is my first time working with a relational field. I managed to set up the query and get the name and photo of the columnists. But I can’t return the titles of the news along with their respective tags. News titles and tags end up repeating themselves in the loop.

    $args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => 5 );

    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();
    $posts = get_field(‘colunista’);
    if( $posts ): ?>

    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    <?php setup_postdata($post); ?>
    <div class=”col-3″>” class=”fotoColunista”></div>
    <div class=”containerInformacaoesColunistas col-8″>
    <span>/assets/imgs/icon-colunista.svg” class=”iconeNomeColunista”></span><div class=”nomeColunista”><?php the_title(); ?></div>

    <?php endforeach; ?>
    <?php endif; ?>

    <?php wp_reset_postdata(); // IMPORTANT – reset the $post object so the rest of the page works correctly ?>
    <div class=”palavraChaveColunista”><?php the_field(‘palavra_chave_noticia’) ?></div>
    <div class=”textomateriaColunista”><?php echo mb_strimwidth( get_the_title(), 0, 60, ‘…’ ); ?></div>
    <!–Final relationship–>

    <?php endwhile; ?>

  • You need to edit you post and use code tags instead of <blockquote>, the forum stripped out parts of you code.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.