M
M
Maxim Stepanov2015-10-16 20:26:50
Taxonomy
Maxim Stepanov, 2015-10-16 20:26:50

How to display custom taxonomy posts in current category?

Hello!
I created a taxonomy with categories in it. Categories have several levels of nesting. I have displayed a list of all categories on the site, and I want to make sure that by clicking on each category, exactly those posts that belong only to it are displayed. I created some code using wp_query. But it works only partially, displaying all posts from all categories. In theory, you need to use tax_query (it is commented out) to filter out the ones you need. In it, in theory, in terms, you need to specify the id of the current category (the value of which is zero), but for some reason the code stops working with tax_query. Can anyone suggest what I'm doing wrong?

<?php $query = new WP_Query( $args ); ?>
<?php $category_id = get_cat_ID(); ?>
<?php 
  $category_id = get_cat_ID();
  $args = array(
    'post_type' => 'company',
    'posts_per_page' => 10,


/*
    'tax_query' => array(
      array(
        'taxonomy' => 'company_category',
        'field' => 'id',
        'terms' => $category_id
      )
    )
*/


  );
  $query= new WP_Query( $args );
?>

<?php if ($query->have_posts() ) : ?>									 
  <?php while ( $query->have_posts() ) : $acsessuar->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <a href="<?php echo get_permalink(); ?>"><?php echo get_the_title( $ID ); ?></a>
    </div>
  <?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Stepanov, 2015-10-17
@badsanta2008

The question is closed.
For those who are interested, here is the solution code.

<div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 col-xxs-12">
              <h1><span><?php single_cat_title('Каталог: '); ?> id=<?php echo $category_id = get_cat_ID();?></span></h1>
                <?php $cat = get_term_by('name', single_cat_title('',false), 'company_category'); echo $catSlug = $cat->slug; ?>
            </div>
          </div>
          


          <div class="row">
            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12 col-xxs-12 ">
            
              <?php
                $mypost = array(
                  'post_type' => 'company',
                  'posts_per_page' => 15,
                  'tax_query' => array(
                    array(
                      'taxonomy' => 'company_category',
                      'field' => 'slug',
                      'terms' => $catSlug
                    ),
                  ),
                );
                $loop = new WP_Query( $mypost );
              ?>
              <?php while ( $loop->have_posts() ) : $loop->the_post();?>
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                  <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <a href="<?php echo get_permalink(); ?>"><?php echo get_the_title( $ID ); ?></a>
                  </div>
                </article>
              <?php endwhile; ?>
              
            </div>
          </div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question