0

I will be writing Posts about each of Product's Product Categories (product_cat on products) and I am choosing to group (associate) Posts by the product_cat hierarchical taxonomy. To do this without duplicating the taxonomy, I registered it in my functions.php like so:

add_action( 'init', 'register_product_cat_taxonomy_for_post_object' );
function register_product_cat_taxonomy_for_post_object() {
     register_taxonomy_for_object_type( 'product_cat', 'post' ); 
}

Much like Category archives, I happen to be using woocommerce which by default lets users browse Product Archives by Product Category. They simply click the provided product_cat breadcumbs and i believe archive-product.php will display a list of products that have a product_cat matching the clicked-on breadcrumb.

I am having trouble with my next objective. I plan to hook-into the archive-product.php do_action( ‘woocommerce_after_main_content’ ); to inject some custom funtionality, but I am having trouble writing the custom functionality. I am looking for help writing two different query/display codes to show:

1) posts whose list of product_cat slugs match at minimum the list of product_cat slugs in the breadcrumbs mentioned above. When I say “posts whose…prouduct_cat slugs” refer to the register_product_cat_taxonomy_for_post_object() function above for clarification.

2) posts whose list of product_cat slugs match exactly the list of product_cat slugs in the breadcrumbs mentioned above. When I say “posts whose…prouduct_cat slugs” refer to the register_product_cat_taxonomy_for_post_object() function above for clarification.

Ultimately, the goal is for me to write posts, assign posts specific product_cat terms, then figure out #1 and #2 so that users who are breadcrumb-browsing products by product category can also see (via my injection of #1 and #2) posts related to the product category that they are currently breadcrumb-browsing in archive-product.php

Thank you,

  • In a nutshell you want to show the posts related to current products product_cat below WooCommerce main content, right ? – CodeMascot Dec 13 '16 at 3:35
  • Yeah, but i dont want to hardcode the posts into that injection-location. I am looking for a programmatic way to build two queries 1) and 2) in OP above. – Peter Noges Dec 13 '16 at 5:08
0

Here I've written a function for you-

function the_dramatist_get_product_cat_posts( $args = array() ) {
    $default_args = array(
        'taxonomy' => 'product_cat',
        'get_post_type' => 'post'
    );
    $t = array();
    $param = wp_parse_args($args, $default_args);
    $prod_terms = get_the_terms(get_the_ID(), $param['taxonomy']);
    foreach ($prod_terms as $prod_term ) {
        $t[] = $prod_term->term_id;
    }
    $posts = get_posts(array(
        'post_type' => $param['get_post_type'],
        'numberposts' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => $param['taxonomy'],
                'field' => 'id',
                'terms' => $t,
                'include_children' => false
            )
        )
    ));

    return $posts;
}

By calling this function like the_dramatist_get_product_cat_posts() you'll get all the posts related to current product's product_cat taxonomy terms. It will give you all the post object. Then you can run a foreach or for loop to organize the posts as you like. You can temporarily do a print_r on this function to understand the returned data structure. It'll help you to understand the data.

Hope that helps.

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.