A
A
Alexander Nikolaev2018-10-05 04:25:28
WordPress
Alexander Nikolaev, 2018-10-05 04:25:28

How to display custom post types associated with taxonomy (WordPress)?

The structure is like this:

  • Category
  • -Chapter
  • --Product

Category is a taxonomy (Taxonomies)
Section is a taxonomy with a parent Category (Taxonomies)
Product is a post type with a parent Section (Post Types)
On the main page I display the main categories.
Then you can go to the category, the sections of this category will be displayed.
Further, when you go to the section, all products (of all sections)
are displayed. I don’t understand how to display the products of my particular section.
-------------------------------------------------- ---
Implemented like this
global $wp_query;
// получаем текущий slug
$now_slug = $wp_query->query["catalogs"];

$args = array(
  'post_type' => 'product',
  'paged' => get_query_var('paged'),
  'order' => 'ASC',
  'posts_per_page' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'catalogs',
      'field' => 'slug',
      'terms' => $now_slug
    )
  )
);
                      
query_posts($args);

But it seems to me that this is a straight crutch crutch. Surely there is an easier way to do this.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question