最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Create 3 Levels of relations with Wordpress

matteradmin11PV0评论

I need to create this type of relation in Wordpress:

  • A Catalog can have many Products
  • A Product can have many Catalogs
  • A Category can have many Products
  • A Product can have only one Category

So when I click on the Catalog, I need to see all the Categories of the Products that I have linked with the Catalog.

E.g:

  • Catalog 1 -> Product 1 [Category 1]
  • Catalog 1 -> Product 2 [Category 1]
  • Catalog 1 -> Product 3 [Category 2]
  • Catalog 2 -> Product 2 [Category 1]

So in the frontend, when I click on Catalog 1, I should see the Categories: - Category 1 - Category 2

Then when I click the Category 1 (of Catalog 1) I should see the Products: - Product 1 - Product 2

If I click on Catalog 2, I should see the Categories: - Category 1

Then when I click the Category 1 (of Catalog 2), I should see the Products: - Product 2

How can I rapresent these relations on Wordpress + ACF?

I need to create this type of relation in Wordpress:

  • A Catalog can have many Products
  • A Product can have many Catalogs
  • A Category can have many Products
  • A Product can have only one Category

So when I click on the Catalog, I need to see all the Categories of the Products that I have linked with the Catalog.

E.g:

  • Catalog 1 -> Product 1 [Category 1]
  • Catalog 1 -> Product 2 [Category 1]
  • Catalog 1 -> Product 3 [Category 2]
  • Catalog 2 -> Product 2 [Category 1]

So in the frontend, when I click on Catalog 1, I should see the Categories: - Category 1 - Category 2

Then when I click the Category 1 (of Catalog 1) I should see the Products: - Product 1 - Product 2

If I click on Catalog 2, I should see the Categories: - Category 1

Then when I click the Category 1 (of Catalog 2), I should see the Products: - Product 2

How can I rapresent these relations on Wordpress + ACF?

Share Improve this question asked Feb 14, 2019 at 9:17 MintendoMintendo 1011 silver badge4 bronze badges 5
  • Is the catalog as a custom taxonomy or a custom page? or just a select input in metabox? – Manyang Commented Feb 14, 2019 at 9:31
  • @Manyang for the moment is a custom post type, but If could be useful to resolve the problem, I can change it to a taxonomy. I'm on analysis phase for the moment, so I can do any change. – Mintendo Commented Feb 14, 2019 at 9:36
  • if you dont have problem to make catalog to taxonomy, it will be easier, because you now have a relationship between category and taxonomy through the product id – Manyang Commented Feb 14, 2019 at 9:40
  • @Manyang yes, no problem to change a catalog from custom post type to taxonomy, but I haven't understood how can I get the categories that are linked to catalog throught products. – Mintendo Commented Feb 14, 2019 at 9:45
  • Wait i will give you answer – Manyang Commented Feb 14, 2019 at 9:46
Add a comment  | 

1 Answer 1

Reset to default 0

I not try this, but I hope this working

function get_list_catalog_category_product(){
  //get all catalog
  $catalogs = get_terms( array(
    'taxonomy' => 'catalogs_term',
    'hide_empty' => false
  ));

  if ( !empty($catalogs) ) :
  //make list catalog
  $output = '<ul>';

  foreach( $catalogs as $catalog ) {

      //write all catalog
      $output.= '<li><a href="'.get_term_link( $catalog ).'">'.$catalog->name.'</a>';

      //find all product related the catalog
      $args = array(
          'post_type' => 'product_post',
          'post_status' => 'publish',
          'tax_query' => array(
              array(
                  'taxonomy' => 'catalogs_term',
                  'field'    => 'term_id',
                  'terms'    => $catalog->term_id
              )
          )
      );
      $products = get_posts( $args );

      //make array of category related the catalog
      $all_category = array();

      //make array of product divided by category related the catalog
      $product_by_cat = array();

      if ( !empty($products) ){
        foreach( $products as $product ) {

          //write every product html
          $products_html ='<li><a href="'.get_permalink($product->ID).'">'.$product->post_title.'</a></li>';

          //find all category of this product
          $product_categories = wp_get_object_terms( $product->ID,  'category' );
          if ( ! empty( $product_categories ) ) {
            foreach($product_categories as $product_category) {

                //write the product html in related category if category never created this code will create or if has been create product html just append
                if(isset($product_by_cat[$product_category->term_id])){
                  $product_by_cat[$product_category->term_id] .= $products_html;
                }else{
                  $product_by_cat[$product_category->term_id] = $products_html;
                }

                //push the category as category related to catalog
                array_push($all_category,$product_category->term_id);
            }
          }

          //remove duplicate category if a product has more than 1 category
          $all_category = array_unique($all_category);
        }
      }

      if ( !empty($all_category) ){
        $output.= '<ul>';
          foreach( $all_category as $the_category ) {
            //write category html in catalog
            $output.='<li><a href="'.get_term_link($the_category).'">'.get_term( $term_id, 'category' )->name.'</a></li>';

            //write all product html to the category
            $output.='<ul>'.$product_by_cat[$the_category].'</ul>';
          }
        $output.= '</ul>';
      }

      $output.= '</li>';
  }
  $output.='</ul>';
  echo $output;
  endif;
}
Post a comment

comment list (0)

  1. No comments so far