V
V
Vladislav Ignatiev2018-02-26 22:27:14
WordPress
Vladislav Ignatiev, 2018-02-26 22:27:14

How to display related posts with the same tags (tags) as the first post?

Hello!
I have a search form, the result of which is displayed on a separate page.
How can I display similar posts (in my case, products), with the same tags (tags) as the found post (product)? You need to display them below after all the others. Help please ... I found solutions on the internet, but I can’t get the posts out in any way.

I use ACF in the output, don't be scared

The code
<?php if ( $query->have_posts() ){ ?>
  
  Результаты: <?php echo $query->found_posts; ?> <br />
  <div class='search-filter-results-list'>
  <?php while ($query->have_posts()){ $query->the_post(); ?>

  <!-- Вывод данных поста -->
  <div class="container">
    <div class="product_description_inside_search clearfix">

      <div class="product_description_inside_img">
        <img src="<?php the_field('image_product') ?>">
      </div> 
      <div class="product_title_search">
      <h1>
        <?php the_field('pharm_name'); ?>
      </h1>
      </div>
      <div class="pharm_permalink">
        <a href="<?php the_permalink(); ?>">Подробнее</a>
      </div>
      <div class="where_buy_search">
        <span id="where_buy"><?php the_field('name_pharmacies'); ?></span><br>
      </div>
      <div class="pharm_price">
        <br><h2>Цена: <?php the_field('pharm_price'); ?></h2>
      </div>
    </div>
  </div>
  <!-- Вывод данных поста -->
      
  <?php } ?>
  </div>
<?php } else { ?>
  <div class='search-filter-results-list' data-search-filter-action='infinite-scroll-end'>
    <span>Нет результатов</span>
  </div>
<?php } ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
po5epT, 2018-02-27
@vladislavignatyev

when displaying all posts, you remember the label of the first one: do not forget that i must be declared and incremented as it should be in the cycle. Then you get posts

get_posts( array("tag__in" => array(tags[0]->term_id)) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question