I have a problem.

<div class="list">
    $categories = get_the_category();
    $category_id = $categories[0]->cat_ID;
    $args = array(
      'posts_per_page' => -1,
      'offset' => 0,
      'orderby' => 'date',
      'order' => 'ASC',
      'cat' => $category_id
    $products = get_posts( $args );
  <?php foreach ( $products as $post ) : setup_postdata( $post ); ?>
    <div class="list__item">
      <a href="<?php the_permalink(); ?>">
          <img src="<?php the_post_thumbnail_url('thumbnails--products'); ?>" alt="">
        <h3><?php the_title(); ?></h3>
  <?php endforeach; wp_reset_postdata(); ?>

If I understand correctly, now I should display posts from the category I am currently in because I am retrieving her ID, but posts from the main category Posts are displayed.


get_the_category returns posts only with the default taxonomy "category", if you are trying to display posts from a custom registered taxonomy you should use get_the_terms

Also get_posts argument category doesn't support custom taxonomies, you should use WP_Query's Taxonomy Query mostly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.