Support

Account

Home Forums General Issues Posts Related to Products Category

Solving

Posts Related to Products Category

  • Is it possible to do such scenario using ACF?
    I have a Woocommerce with categories (Let’s call them Muffins and Cakes), I want to show a number of posts (from blog) in each product category pages (Post A, B and D on Muffins and C, F, G and H on Cakes). So, I make a field group (let’s call it Related to Products’ Categories), type Relational–Taxonomy, located on Post Type equal to Post. That way I can select a product category while editing posts. So far so good.
    However when I put this code in my template it does show anything:

    <?php $related_to_products_categories_ids = get_field( 'related_to_products_categories' ); ?>
    <?php // var_dump( $related_to_products_categories_ids ); ?>

    Am I doing anything wrong?

  • The category field is stored in the post, not on the category. In order to get the posts that are related to the category you need to query the posts.

    
    // get the current category ID
    $queried_object = get_queried_object();
    $term_id = $queried_object->term_id;
    
    // query posts
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => -1, // or whatever number you want to show
      'meta_query' => array(
        array(
          'key' => 'related_to_products_categories',
          'value' => '"'.$term_id.'"',
          'compare' => 'LIKE'
        )
      )
    );
    $my_query = new WP_Query($args);
    if ($my_query->have_posts()) {
      while ($my_query->have_posts()) {
        $my_query->the_post();
        // show post
      }
      wp_reset_postdata();
    }
    
  • Thank you John. I used your code but still no results. I am most definitely missing something.
    Here is my field set up:
    Field Group

    I put the query in archive-product.php and as you can see in action it shows nothing:
    http://bizmoz.in/dev/?product_cat=music

  • Since you’re saving and loading terms you may need to use a tax_query, but this is only a guess.

    
    <?php 
      // get the current category ID
      $queried_object = get_queried_object();
      $taxonomy = $queried_object->taxonomy;
      $term_id = $queried_object->term_id;
      
      // query posts
      $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1, // or whatever number you want to show
        'tax_query' => array(
          array(
            'taxnomy' => $taxonomy,
            'terms' => array($term_id)
          )
        )
      );
      $my_query = new WP_Query($args);
      if ($my_query->have_posts()) {
        while ($my_query->have_posts()) {
          $my_query->the_post();
          // show post
        }
        wp_reset_postdata();
      }
    
  • Sadly it did not work either. Thank you anyway. I appreciate your help.

  • Just to double check

    • You have WC products and product categories
    • You are assigning WC product categories to “Posts”
      On the product category page your are trying to show blog posts that have that product category.
  • it may have something to do with WC and this may not be returning what I think it should be returning

    
      $queried_object = get_queried_object();
      $taxonomy = $queried_object->taxonomy;
      $term_id = $queried_object->term_id;
    

    I don’t know how to test this. Taxonomy should be ‘product_cat’ (i think that the WC taxonomy name) and term_id should be the id of the category.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Posts Related to Products Category’ is closed to new replies.