Suporte » Desenvolvendo com WordPress » Posts relacionados WordPress

  • Resolvido José Firmino

    (@ninho1000)


    Fala galera,

    estou desenvolvendo um tema wordpress, e em uma página eu uso uma função para pegar os posts relacionados, ele pega todos os posts até o atual. Porém ele não precisaria pegar o atual, qual seria a forma de excluir o post atual e mostrar realmente só os relacionados?

    OBS: estou usando um custom_post_type=cursos

    Valeu!!!

    function postagem_relacionada() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && is_wp_error($categories)):
    foreach ($categories as $category):
    array_push($cat_ids, $category->term_id);
    endforeach;
    endif;

    $current_post_type = get_post_type($post_id);
    $query_args = array(

    ‘category__in’ => $cat_ids,
    ‘post_type’ => $current_post_type,
    ‘post_not_in’ => array($post_id),
    ‘posts_per_page’ => ’10’

    );

    $related_cats_post = new WP_Query( $query_args );

    if($related_cats_post->have_posts()):
    while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
    <div class=”card text-gray height-100p shadow-v2″>
    “>
    ” alt=””>

    <div class=”p-4″>
    ” class=”h6″>
    <?php the_title(); ?>

    <button class=”btn btn-primary btn-sm btn-pill” style=”color: #191B31!important”>
    + <?php echo get_field(‘quantidade_de_alunos_formados’) ?> Alunos formados
    </button>
    </div>
    </div>
    <?php endwhile;

    // Restore original Post Data
    wp_reset_postdata();
    endif;

    }

    A página que eu preciso de ajuda: [fazer login para ver o link]

Visualizando 1 resposta (de um total de 1)
  • Criador do tópico José Firmino

    (@ninho1000)

    Resolveu meu problema

    Erro do código: ‘post_not_in’ => array($post_id)
    Solução: ‘post__not_in’ => array($post_id)

    Novo código atualizado para quem precisar fazer um loop de postagens relacionadas e excluir o post atual.

    Basta adicionar no arquivo functions.php

    function postagem_relacionada() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && is_wp_error($categories)):
    foreach ($categories as $category):
    array_push($cat_ids, $category->term_id);
    endforeach;
    endif;

    $current_post_type = get_post_type($post_id);
    $query_args = array(

    ‘category__in’ => $cat_ids,
    ‘post_type’ => $current_post_type,
    ‘post__not_in’ => array($post_id),
    ‘posts_per_page’ => ’10’

    );

    $related_cats_post = new WP_Query( $query_args );

    if($related_cats_post->have_posts()):
    while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
    <div class=”card text-gray height-100p shadow-v2″>
    “>
    ” alt=””>

    <div class=”p-4″>
    ” class=”h6″>
    <?php the_title(); ?>

    <button class=”btn btn-primary btn-sm btn-pill” style=”color: #191B31!important”>
    + <?php echo get_field(‘quantidade_de_alunos_formados’) ?> Alunos formados
    </button>
    </div>
    </div>
    <?php endwhile;

    // Restore original Post Data
    wp_reset_postdata();
    endif;

    }

    Abraço a todos!!

Visualizando 1 resposta (de um total de 1)
  • O tópico ‘Posts relacionados WordPress’ está fechado para novas respostas.